标签:
01作业
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace _01var作业推断类型 { class Program { static void Main(string[] args) { #region int和string系统已经实现了IComparable接口的ComparaTo方法 //System.Collections.ArrayList list = new ArrayList(); //list.Sort(); //string s = "ABCDEF"; //string s1 = "abcdef"; //Console.WriteLine(s.CompareTo(s1)); //Console.WriteLine(string.Compare("a", "dfg")); //int n = 10; //int m = 34; //Console.WriteLine(n.CompareTo(m)); //Console.ReadKey(); #endregion //var s; Error.隐式类型的变量必须初始化.因为编译器没法根据右侧进行类型推断。 //var s = 23; //s = "";错误,无法将类型string隐式转换为int //Console.WriteLine(s.GetType()); //装箱是值类型到引用类型,拆箱是引用类型到值类型。 //把一个string赋给object既不是装箱也不是拆箱。他俩都是引用类型,这就类型转换 //object s3 = 3434; //Console.WriteLine(s3.GetType()); //Console.ReadKey(); //1.var是类型推断;编译器在编译时已经将var替换成了对应的数据类型。IL中间语言已经将他们变为int,string,Person //2.var只能是局部变量,(方法中声明的变量),不能用作类的成员变量,不能用作方法的返回值,不能用作方法的参数。 //var s; Error.隐式类型的变量必须初始化.因为编译器没法根据右侧进行类型推断。 //非泛型集合不能进行类型推断,泛型集合可以。 var n = 100; Console.WriteLine(n); var m = "sdf"; Console.WriteLine(m); var p = new Person(); Console.WriteLine(p.ToString()); Console.ReadKey(); //1.使用var声明变量与直接使用对应的类型声明变量是完全一样的 //int n = 3; var m = 3;完全一样 //2.C#中的var与js的var完全不一样,C#中的var依然表示强类型,而js中的var是弱类型,且总是用它定义变量 //3.强类型是在编译时(反编看看IL生成了什么)就已经能确定类型。弱类型是运行时才确定类型。js中 var i = 34; 然后还可以继续动态改变类型i= "ffdf";C#就不一样了,以后不能再赋其他值。 } } public class Person { public string Name { get; set; } public int Age { get; set; } } }
02装箱拆箱
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Diagnostics; namespace _02装箱和拆箱 { public class Program { static void Main(string[] args) { #region 装箱和拆箱 //装箱是值类型到引用类型,拆箱是引用类型到值类型。 //把一个string赋给object既不是装箱也不是拆箱。他俩都是引用类型,这叫类型转换 //int n = 100; //string s = Convert.ToString(n); //没有发生装箱.因为这个方法和装箱有本质的区别。Convert是根据n先在堆中创造一个引用类型,将100装进去,然后让s指向这个地址。装箱是从拷贝到堆中。 //int m = int.Parse(s); //没有发生拆箱。Parse是根据字符串s直接创建一个整型,然后return到栈中。然后m来接收。 //Console.ReadKey(); //======上面通过调用Convert和Parse方法转型与拆箱装箱的区别=====装拆箱拷贝与Convert、Parse指针指向、新创建对象的区别!!==================== //========装拆箱还是比较耗费资源的,效能低==但上面的方法直接创建新对象难道就不耗费资源吗======= //int n = 100; //object o = n; //把值类型赋给引用类型,这叫一次装箱。也就是值类型直接拷贝到堆里面,然后o指向这个引用类型的变量 //int m = (Int32)o; //把引用类型赋给值类型,要先拆箱,显示转换一下。拆箱是把应用类型的值拷贝到栈里面。从堆扔到栈。如果没有指针再指向它,就等GC吧 //int i = o as Int32; as必须用于引用类型或可以为null的类型 //注意,装的时候用什么装的,拆的时候就得用哪个类型拆。。其实拆箱的代码部分只是右边的(Int32)o //Console.WriteLine(o.GetType()); //事实证明o变成了int32 //Console.ReadKey(); //Console.WriteLine("=============判断下面几行代码是否发生的装拆箱===========") //int n = 100; //Console.WriteLine(n); //wl方法有19个重载,反编译IL看到: L_0005: call void [mscorlib]System.Console::WriteLine(int32)//因此没有拆装箱 //Console.WriteLine("张:{0},林:{1},李:{2}", 23, 23, 45);//装箱了。L_0025: call void [mscorlib]System.Console::WriteLine(string, object, object, object) //Console.ReadKey(); //=================================== //int n = 100; //object o = n; //Console.WriteLine(n+""+(int)o); //这儿会调用Contact方法,因此装箱两次。String+=也一样,都会装箱 //Equals是判断内容是否相同 #endregion #region 装箱和拆箱的性能问题 //==========非泛型集合的拆装箱耗时耗资源 ArrayList arrInt = new ArrayList(); Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 10000000; i++) { arrInt.Add(i); } watch.Stop(); Console.WriteLine(watch.Elapsed); //=======最好避免频繁的拆装箱===那就用泛型集合 List<int> arrInt2 = new List<int>(); Stopwatch watch2 = new Stopwatch(); watch2.Start(); for (int i = 0; i < 10000000; i++) { arrInt2.Add(i); } watch2.Stop(); Console.WriteLine(watch2.Elapsed); Console.ReadKey(); #endregion } } public class Person { public string Name { get; set; } public int Age { get; set; } } }
03自定义泛型
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _03自定义泛型 { public class Program { static void Main(string[] args) { MyClass<string, int, decimal, byte, string, object> mc = new MyClass<string, int, decimal, byte, string, object>(); mc.SayHi("dsfdsfdsf"); Console.ReadKey(); } } //1.泛型类 public class MyClass<T,K,V,W,Y,Z> { public void SayHi(T arg) { Console.WriteLine(arg); } } //2.泛型方法 public class Class1 { public void Sayello<T>(T msg) { Console.WriteLine(msg); } } //3.泛型接口 public interface IFace<T> { T SayHi(); void SayHello(T msg); } //实现泛型接口 //1.普通类实现泛型接口 public class Class2 : IFace<string> { public string SayHi() { throw new NotImplementedException(); } public void SayHello(string msg) { throw new NotImplementedException(); } } //2.泛型类实现泛型接口 public class Class3<U> : IFace<U> //这里的IFace<U>用泛型类中的U代替;当然IFace<U>中的U可以用其他类型stirng、int等 { public U SayHi() { throw new NotImplementedException(); } public void SayHello(U msg) { throw new NotImplementedException(); } } } Powered by VisualSVN Ser
标签:
原文地址:http://www.cnblogs.com/haust/p/4424684.html