Initialize an Anonymous Type Using Object Initialization in CSharp

Description

The following code shows how to initialize an Anonymous Type Using Object Initialization.

Example


using System;//ww  w . j a  va2 s .  c  o m
public class MainClass {
    public static void Main() {

        var address = new {
            address = "First Street",
            city = "Vancouver",
            state = "GA",
            postalCode = "99999"
        };

        Console.WriteLine("address = {0} : city = {1} : state = {2} : zip = {3}",
         address.address, address.city, address.state, address.postalCode);
        Console.WriteLine("{0}", address.GetType().ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var