Exemple #1
0
 public static void ModifyNewCoord(ref CoordinateClass c)
 {
     c.x = 99;
     c.y = 59;
 }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello,C#!");
            //value and reference
            //srtuct pass value
            Coordinate location;
            location.x = 10;
            location.y = 10;
            AttempTomodifyCoord(location);
            Console.WriteLine("({0},{1})",location.x,location.y);
            ModifyCoord(ref location);
            Console.WriteLine("({0},{1})", location.x, location.y);
            //object pass reference
            CoordinateClass newlocation=new CoordinateClass();
            newlocation.x = 10;
            newlocation.y = 10;
            AttempTomodifyNewCoord(newlocation);
            Console.WriteLine("({0},{1})", newlocation.x, newlocation.y);
            ModifyNewCoord(ref newlocation);
            Console.WriteLine("({0},{1})", newlocation.x, newlocation.y);
            //enum
            Days meetingDay = Days.Tuesday | Days.Thursday;
            Console.WriteLine(meetingDay.ToString());
            //装箱转换
            ///当值类型被作为引用类型的参数传给方法或者赋值给一个引用类型参数时,堆上将创建一个复制值到值字段的对象
            int personID = 111;
            object boxID = personID;
            personID = 222;
            int unboxID = (int)boxID;
            Console.WriteLine(personID.ToString());
            Console.WriteLine(unboxID.ToString());

            ///foreach循环中无法改变集合对象,会直接编译器报错
            /// 在Java中亦是,只是不会编译报错
            String[] stringArray = new String[5];
            for (int i=0;i<stringArray.Length;i++)
            {
                stringArray[i] = i.ToString();
                Console.WriteLine(stringArray[i]);
            }
            foreach (String s in stringArray)
            {
                ;///	s = "hh";///语法报错
            }

            //C#3.0初始化器
            //class成员变量的set get写法
            Person person = new Person();
            person.id = 2;
            person.name = "hh";
            Console.WriteLine(person.name);
            //等同于使用初始化器如下
            Person person2 = new Person { id = 2, name = "hh" };
            Console.WriteLine(person2.name);

            //as和is操作符
            //is只是返bool变量表示是否可以转换成给定类型
            //as操作则直接将其转换成给定类型,若失败则返回null
            //这里说的转换可能是引用转换,也可能是装箱或拆箱操作
            Student student = new Student { id=2,name="hhh",school="ss"};
            student.school = "Nanjing University";
            Console.WriteLine("student {0} a person",student is Person?"is":"is not");
            Console.WriteLine("{0}",student.school);

            Person personFromStudent = student as Person;
            if (personFromStudent!=null) { Console.WriteLine(personFromStudent.ToString()); }
            //此时观察personFromStudent的属性,已经没有school这一项了,说明as操作同时进行了类型转换装箱操作
            //Console.WriteLine("personFromStudent {0} a Student", personFromStudent is Student?"is":"is not");

            //Babe类用于演示C#6.0自动实现属性的初始化器
            Baby babe1 = new Baby { name="sss"};
            Console.WriteLine(babe1.bornTime);
        }
Exemple #3
0
 public static void AttempTomodifyNewCoord(CoordinateClass c)
 {
     c.x = 50;
     c.y = 90;
 }