Ejemplo n.º 1
0
    public static void Main()
    {
        StructDemo sd1 = new StructDemo();

        sd1.Id   = 101;
        sd1.Name = "Mark";

        StructDemo sd2 = sd1;

        sd2.Id   = 102;
        sd2.Name = "Marry";

        Console.WriteLine("sd1.Id = {0}, sd1.Id = {1}, && sd2.Name = {2}, sd2.Name = {3}", sd1.Id, sd1.Name, sd2.Id, sd2.Name);

        ClassDemo cd1 = new ClassDemo();

        cd1.Id   = 101;
        cd1.Name = "Mark";

        ClassDemo cd2 = cd1;

        cd2.Id   = 102;
        cd2.Name = "Marry";

        Console.WriteLine("cd1.Id = {0}, cd1.Id = {1}, && cd2.Name = {2}, cd2.Name = {3}", cd1.Id, cd1.Name, cd2.Id, cd2.Name);
    }
Ejemplo n.º 2
0
    public static void Main()
    {
        StructDemo sd1 = new StructDemo(101, "Mark");

        Console.WriteLine("Id is = {0} and Name = {1}", sd1.Id, sd1.Name);

        StructDemo sd2 = new StructDemo();

        sd2.Id   = 102;
        sd2.Name = "John";
        Console.WriteLine("Id is = {0} and Name = {1}", sd2.Id, sd2.Name);

        /*
         *      Object initilizer syntax, introduced in C# 3.0 can be used to initilize eighter struct or a class.
         */

        StructDemo sd3 = new StructDemo {
            Id   = 103,
            Name = "Sara"
        };

        Console.WriteLine("Id is = {0} and Name = {1}", sd3.Id, sd3.Name);
    }