The anonymous types feature results the combination of Implicitly Typed Variables feature and Object Initializer feature.
Usually, every time I need to create a new type I have to create a class and add some logic:
public class PropertiesSample
{
private string m_Param1;
private string m_Param2;
public string Param1
{
get
{
return m_Param1;
}
set
{
m_Param1 = value;
}
}
public string Param2
{
get
{
return m_Param2;
}
set
{
m_Param2 = value;
}
}
}
And every time I need to use the new type I have to do something like these:
PropertiesSample sample = new PropertiesSample();
sample.Param1 = TextBoxProperties1.Text;
sample.Param2 = TextBoxProperties2.Text;
To use the anonymous types feature I need to declare and Implicitly Typed Variable "var sample" and create a new instance of "Something" and then initialize the new instance using Object Initializer feature passing every thing I need.
for example:
var sample = new { Param1 = TextBoxProperties1.Text, Param2 = TextBoxProperties2.Text };
With this line of code the compiler will automatically generate the following type and the following type initialization:
public class _SomeClass
{
private string field1;
private string field2;
public string Param1
{
get
{
return field1;
}
set
{
field1 = value;
}
}
public string Param2
{
get
{
return field2;
}
set
{
field2 = value;
}
}
}
_SomeClass sample = new _SomeClass
{
Param1 = TextBoxProperties1.Text;
Param2 = TextBoxProperties2.Text;
}