Beispiel #1
0
 public static string ObjectInitialiserClassToString
     (this ObjectInitialiserClass obj)
 {
     return
         (obj.MyString + " "
          + obj.MyInt.ToString() + " "
          + obj.MyDateTime.ToLongDateString());
 }
Beispiel #2
0
        private static void DemoExtensionMethods()
        {
            ObjectInitialiserClass c =
                new ObjectInitialiserClass()
            {
                MyDateTime = DateTime.Now,
                MyInt      = 6,
                MyString   = "hello"
            };

            //extension method call
            string returnMessage = c.ObjectInitialiserClassToString();
        }
Beispiel #3
0
        private static void DemoObjectInitializer()
        {
            //you do this in C# prior to 3.0
            ObjectInitialiserClass c = new ObjectInitialiserClass();

            c.MyDateTime = DateTime.Now;
            c.MyInt      = 5;
            c.MyString   = "hello di";

            //in C# 3.0, you can do this
            ObjectInitialiserClass c2 =
                new ObjectInitialiserClass()
            {
                MyDateTime = DateTime.Now,
                MyInt      = 6,
                MyString   = "hello"
            };
        }
Beispiel #4
0
        private static void DemoCollectionInitializer()
        {
            //you do this in C# prior to 3.0
            ObjectInitialiserClass c = new ObjectInitialiserClass();

            c.MyDateTime = DateTime.Now;
            c.MyInt      = 5;
            c.MyString   = "hello di";


            ObjectInitialiserClass c2 = new ObjectInitialiserClass();

            c2.MyDateTime = DateTime.Now;
            c2.MyInt      = 6;
            c2.MyString   = "hello di2";


            ObjectInitialiserClass c3 = new ObjectInitialiserClass();

            c3.MyDateTime = DateTime.Now;
            c3.MyInt      = 7;
            c3.MyString   = "hello di3";


            List <ObjectInitialiserClass> l = new List <ObjectInitialiserClass>();

            l.Add(c);
            l.Add(c2);
            l.Add(c3);


            //in C# 3.0, you can do this
            List <ObjectInitialiserClass> l2 =
                new List <ObjectInitialiserClass>()
            {
                c, c2, c3
            };
        }