Felix  RuthenbergAnonymousTypes and their properties

When you instantiate a new object using the keyword new without actually giving it a name (see below), the C# compiler automatically generates an anymous type.

static void Main(string[] args)

  var anoType = new { FName = "Felix", LName = "Ruthenberg" };
  Console.WriteLine("FName: {0} LName: {1}",anoType.FName,anoType.LName);

This type has two properties (called key properties, not fields!) which are read-only. That means that the framework creates "on-the-fly classes" and istantiates the object as shown in the example.

Refer to the MSDN websites for more details: http://msdn.microsoft.com/en-us/library/bb397696.aspx

Unfortunately I couldn't find a reference or explanation why the property values cannot be changed once they have been set. If you find out anything about that please just leave me a comment.

Kategorien: C#; 21.01.2009 09:51:05


Neuen Kommentar einfügen:




© Copyright 2008 ppedv AG