Interview question - anonymous types

Camillia
Camillia used Ask the Experts™
on
I've been reading about anonymous types and I think the answer to this question is the 4th one "new class with properties both set and get methods". Saw an explanation on a site but forgot to bookmark it

b3.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Gotta love it when people try intentionally to screw you up by giving you two very close answers.

The answer is #3 - with only get methods.  

This seems like a terrible interview question, though. Usually means the interviewer is trying to test your academic knowledge instead of practical knowledge, which doesn't help when you get into the job itself (unless your job is to build a compiler).
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
You cannot assign to the properties that are defined on an anonymous type, therefore the properties are "get" only. And anonymous types are always classes.
Thanks, guys. Taking tests are becoming routine for job interviews now, seems like it.
Commented:
A new class with properties and only get methods.  Proof of concept -
using System;

namespace EE_Q29137622
{
    class Program
    {
        static void Main(string[] args)
        {
            var paul = new { ID = 1, FirstName = "Paul", LastName = "Tester", IsWorking = true };
            Console.WriteLine($"paul is a {paul.GetType()}");
            Console.WriteLine($"paul's value is {paul}");
            paul.FirstName = "Larry";
            paul.LastName = "Johnson";
            paul.IsWorking = false;
            Console.ReadLine();
        }
    }
}

Open in new window

Produces the following error -Capture.PNGWhile the following:
using System;

namespace EE_Q29137622
{
    class Program
    {
        static void Main(string[] args)
        {
            var paul = new { ID = 1, FirstName = "Paul", LastName = "Tester", IsWorking = true };
            Console.WriteLine($"paul is a {paul.GetType()}");
            Console.WriteLine($"paul's value is {paul}");
            Console.ReadLine();
        }
    }
}

Open in new window

Produces the following output -Capture.PNG-saige-
Thanks for the proof of concept. Going to try that. Helps me understand the solution better.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial