Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            var classExample     = new ClassExample(true, 111, 55.44);
            var classExampleCopy = classExample;

            classExample.Field1 = false;
            classExample.Field2 = 222;
            classExample.Field3 = 111.222;

            Console.WriteLine("Пример присвоения ссылки.");
            Console.WriteLine(classExample.Info);
            Console.WriteLine(classExampleCopy.Info);

            var structExample     = new StructExample(true, 111, 55.44);
            var structExampleCopy = structExample;

            structExample.Field1 = false;
            structExample.Field2 = 222;
            structExample.Field3 = 111.222;

            Console.WriteLine("Пример присвоения значения.");
            Console.WriteLine(structExample.Info);
            Console.WriteLine(structExampleCopy.Info);

            Console.ReadLine();
        }
Exemplo n.º 2
0
        void button_Click(object sender, EventArgs e)
        {
            string s = (sender as Button).Text;

            switch (s)
            {
            case "例1":
                Student student = new Student();
                student.Name = "张三";
                student.Age  = "21";
                student.PrintInfo();
                MessageBox.Show(student.Output);
                break;

            case "例2":
                StructExample p = new StructExample();
                break;

            case "例3":
                MethodExample1 q = new MethodExample1();
                MessageBox.Show(q.GetOutPut());
                break;

            case "例4":
                MethodExample2 r = new MethodExample2();
                MessageBox.Show(r.GetOutPut());
                break;

            case "例5":
                int x = 13, y = 3;
                int result, rem;
                MethodExample3.Div(x, y, out result, out rem);
                string s1 = string.Format("x={0},y={1}\n商={2},余数={3}", x, y, result, rem);
                MessageBox.Show(s1);
                break;

            case "例6":
                string r1 = string.Format("1,2,3,5的平均值为{0}", MethodExample4.Average(1, 2, 3, 5));
                string r2 = string.Format("4,5,6的平均值为{0}", MethodExample4.Average(4, 5, 6));
                MessageBox.Show(r1 + "\n" + r2);
                break;

            case "例7":
                InheritExample m7 = new InheritExample();
                m7.ShowDialog();
                break;
            }
        }
Exemplo n.º 3
0
    public void SerializeStruct()
    {
        const string expected = @"Count,Flag,Description
5,True,""This is the description""";

        var input = new StructExample
        {
            Count       = 5,
            Flag        = true,
            Description = "This is the description"
        };

        var result = CsvConvert.Serialize(input);

        Assert.That(result, Is.EqualTo(expected));
    }
Exemplo n.º 4
0
        private static void Main(string[] args)
        {
            var classExample = new ClassExample(true, 111, 55.44);

            ClassMethod(classExample);

            Console.WriteLine("Пример передачи в метод экземпляра класса.");
            Console.WriteLine(classExample.Info);

            var structExample = new StructExample(true, 111, 55.44);

            StructMethod(structExample);

            Console.WriteLine("Пример передачи в метод экземпляра структуры.");
            Console.WriteLine(structExample.Info);
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Man man = new Man();
            // Вызов метода GetType() (Метод базового класса object).
            var type = man.GetType();

            Console.WriteLine("Информация о классе");
            GetMainInfoAboutClass(type);
            Console.WriteLine();

            type = typeof(EnumExample);
            Console.WriteLine("Информация о перечислении");
            GetMainInfoAboutClass(type);
            Console.WriteLine();

            StructExample structExample = new StructExample();

            type = structExample.GetType();
            Console.WriteLine("Информация о структуре");
            GetMainInfoAboutClass(type);
            Console.WriteLine("");

            Console.ReadLine();
        }
Exemplo n.º 6
0
 // Структуры копируют значение.
 private static void StructMethod(StructExample strucExample)
 {
     strucExample.Field1 = false;
     strucExample.Field2 = 222;
     strucExample.Field3 = 111.222;
 }
Exemplo n.º 7
0
        // This is basically a runner class, runs various classes implemented.
        public static void Main(string[] args)
        {
            //var datalist = new StoreData<int>();
            //datalist.Add(1);
            //datalist.Add(2);
            //datalist.Add(3);
            //datalist.Add(4);
            //datalist.Add(5);

            //foreach (var item in datalist)
            //{
            //    Console.WriteLine(item);
            //}

            //var datalist2 = new StoreData<string>();
            //datalist2.Add("If ");
            //datalist2.Add("Only ");
            //datalist2.Add("I ");
            //datalist2.Add("Could ");
            //datalist2.Add("***.");

            //foreach (var item in datalist2)
            //{
            //    Console.Write(item);
            //}
            //Console.ReadLine();


            A dataA = new A()
            {
                Data1 = "Hello", Data2 = "World"
            };
            // Assigning like this does a "shallow copy".
            A dataB = dataA;

            dataB.Data1 = "Goodbye";
            dataB.Data2 = "City";

            A dataBDistinct = new A(dataA);

            // dataBDistinct should not be affected by changes made to dataA
            dataA.Data1 = "Hello";
            dataA.Data2 = "World";

            StructExample someStruct      = new StructExample();
            StructExample someOtherStruct = new StructExample(1, 2);

            //FileInfo fi = new FileInfo("ads");
            //fi.Attributes = FileAttributes.ReadOnly | FileAttributes.ReadOnly;

            Type predefineAttribute = typeof(AttributeUsageAttribute);
            Type conditionalAttr    = typeof(ConditionalAttribute);

            foreach (Attribute attr in  predefineAttribute.GetCustomAttributes(false))
            {
                Console.WriteLine(attr.ToString());
            }
            foreach (PropertyInfo poperty in predefineAttribute.GetProperties())
            {
                Console.WriteLine(poperty.ToString());
            }

            SomeObsoleteMethod();

            IMyInterfaceCompression compression = new ZipCompression();

            compression.Compress("", null);
            compression = new RarCompression();
            compression.Compress("", null);

            Console.WriteLine("***************Async****************");
            TestAsync();
            // Console.WriteLine("***************Async****************");

            Console.WriteLine("***************LINQ EXAMPLE ****************");
            LinqExample db        = new LinqExample();
            var         customers = from cust in db.Customers
                                    select cust;

            db.AddCustomer(new Customer()
            {
                Name = "NitinAr", Designation = "Technical Fellow", City = "Seattle"
            });
            foreach (Customer cst in customers)
            {
                Console.Write(cst.Name + ", ");
            }
            Console.WriteLine();
            Console.WriteLine("***************LINQ EXAMPLE ****************");

            Console.WriteLine("*************** DELEGATES, LAMBDAS ****************");
            Func <int, int, bool> comparisonHandler = LessThan;

            comparisonHandler = (a, b) => a < b;
            comparisonHandler = delegate(int a, int b)
            {
                return(a < b);
            };
            Console.WriteLine("***************DELEGATES, LAMBDAS****************");

            Console.WriteLine("***************vs EXPRESSIONS****************");
            // Expression tree is a collection of Data and by iterating over data, it is possible to convert
            // data to another type.
            // Look at the different in calling ToString() on Func<> v/s Expression<>.
            Expression <Func <int, int, bool> > comparisonHandlerExpression;

            comparisonHandlerExpression = (x, y) => x < y;

            Console.WriteLine(comparisonHandler.ToString());
            Console.WriteLine(comparisonHandlerExpression.ToString());
            Console.WriteLine(comparisonHandlerExpression.Body.ToString());
            Console.WriteLine("***************vs EXPRESSIONS****************");

            CloseOverMe     closeOverMe = new CloseOverMe();
            Func <int, int> incrementer = closeOverMe.GetFuncIncrementer();

            Console.WriteLine(incrementer(3));
            Console.WriteLine(incrementer(4));

            Console.ReadLine();
        }