Answer the question
In order to leave comments, you need to log in
Anonymous types in c# and their features?
Hello. Started learning about anonymous types in c#.
The author gives an example of the Anonymous type syntax var instance=new{Name"Alex", Age=27};
and proposes to add additional syntax elements to this line step by step - so that this line becomes more recognizable "readable" for us (I remind you that the author does this because, for example, the entire course is educational) t .e turning the above line into var instance=new MyClass(){Name"Alex", Age=27};
- saying that we added the name of the default constructor and parentheses to accept constructor arguments, and immediately demonstrating that by adding this - the studio suggests generating properties for Age and for Name in the MyClass class (the class it created it himself, again for an example, but said that in anonymous types, when generating properties, the class is also created automatically)
Auto-implemented properties were generated in the class
- but the author said that they should be read-only - that is, in (well, in this case, you just need to remove set - after all, the studio generated properties as for ordinary class fields when we added the name of the default constructor after the new keyword).
And that brings me to question #3 (questions #1 and 2 are below) If auto-generated, auto-implemented properties in an also auto-generated class (let's call it MyClass) are read-only, then how in the initializer block on the line var instance=new{Name"Alex", Age=27};
- we generally we can appropriate to these fields of value?
Here is a screenshot of the first example, in which I am interested in a comment: - in which I have all these questions.
question #1 i.e. does the compiler create a new name for an anonymous type every time, which in turn is a reference type ?
question number 2, i.e. the application cannot access the reference (because the type is reference) to the new name created
Further, the author says that the compiler.
Question number 3 - presented at the beginning.
Answer the question
In order to leave comments, you need to log in
This author has only greatly confused you. Code:
Equivalent to this code:
class Anonymous0001 // ссылочный тип
{
public string Name { get; private set; } // из других классов выглядит как read-only свойство
public int Age { get; private set; }
public Anonymous0001(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main()
{
var instance = new Anonymous0001("Alex", 27);
}
}
var a = new System.Collections.Generic.Dictionary<string, int>() { { "vasya", 0 }, { "kolya", 0 }, { "alex", 1} };
int i = 0;
var result = from item in a where item.Value == 0
select new { Index = i++, Name = item.Key, Id = item.Value }; // создание объекта анонимного типа
foreach(var res in result) {
bool first = true;
foreach (var prop in res.GetType().GetProperties()) {
if (first) first = false;
else Console.Write(", ");
Console.Write("{0} = {1}", prop.Name, prop.GetValue(res, null));
}
Console.WriteLine();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question