Anonymous Types

Added in .NET 3.5
This provides a quick way to create a single read-only object.
Provides a way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first
This is achieved by using implicitly typed local variables.
The name/value pairs declare properties with initial values.
Although anonymous types are classes they do not implement the IDisposable interface

var myType = new { Property_Name="Simon", Property_Age=30 }; 

instead of

public class ReadOnly_Object { 
   public string Property_Name { get; }
   public int Property_Age { get; }
var myType = new ReadOnly_Object();
myType.Property_Name = "Simon";
myType.Property_Age = 30;

If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them.
It is derived from System.Object class and it is also a sealed class.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext