C # ref out is as simple to understand and use case

ref and out of the difference between:

  • Ref parameter must be initialized to the methods, parameters must be assigned out of the method in
 int a = 10;
 int b = 20;
ds(ref a,ref b);//ref外部初始化,传递值类型引用地址
Console.WriteLine(a);
Console.WriteLine(b);
Console.ReadKey();
        }
        public static void ds(ref int x,ref int y)
        {
            x = 20;//不使用第三变量,互变值
            y = 10;
        }
        //-----------------------OUT------------------------
            int a ;
            int b ;
            ds(out a, out b);//在这个函数体内进行赋值
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.ReadKey();
        }
        public static void ds(out int x,out int y)
        {
            x = 20;//方法体内赋值
            y = 10;
        }

and as is the type of conversion:

  • It can be used to determine whether a given object is a compatible type.
  • as is the conversion between the two references, the success of the assignment, or null if unsuccessful
//AS使用
   using (FileStream fs = new FileStream("person.xml", FileMode.Open, FileAccess.ReadWrite))
            {
                //创建xml序列化器
                XmlSerializer xml = new XmlSerializer(typeof(List<Person>));
                List<Person> ulist = xml.Deserialize(fs) as List<Person>;//接收一个文件流,反序列化返回一个Object类型的结果
                this.dataGridView1.DataSource = ulist;
            }
//IS使用
            string str = "10";
            if (str is string)
            {
                Console.WriteLine("可以");
            }
            else
            {
                Console.WriteLine("不可以");
            }
            Console.ReadKey();

Published 21 original articles · won praise 3 · Views 347

Guess you like

Origin blog.csdn.net/MrLsss/article/details/104083654