随笔分类 -  C#

多播委托
摘要:/*多播委托*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { Action operations = MathOperations.MultiplyByTwo; operations += MathOperations.Square; ProcessAndDisplayNumber(operations,20.00);//一次执行多个方法,如果一个方法抛出了异常,那么后面的方法就不会被执行。 //为了避免方法抛... 阅读全文

posted @ 2013-11-28 17:05 wp456 阅读(175) 评论(0) 推荐(0)

委托练习一
摘要:/*委托*/using System;namespace Frank{ public class Test { private delegate string GetAString();//定义委托 private delegate double DoubleOp(double x);//定义第二个委托 public static void Main(string[] args) { GetAString str = new GetAString(1.ToString); GetAString str2 = 1.ToString;//自动类型推断 ... 阅读全文

posted @ 2013-11-28 14:42 wp456 阅读(142) 评论(0) 推荐(0)

params、explicit、可选参数和implicit的使用
摘要:/*params、explicit、可选参数和implicit的使用explicit 显示的implicit 隐士的params 可变参数必须是参数列表最后一个*/using System;namespace Frank{ public class Test { public int Count{get;set;} public static void Main(string[] args) { Test t = new Test(); t.Count = 10; Test2 t2 = new Test2(); t2.Count = 20; ... 阅读全文

posted @ 2013-11-28 10:46 wp456 阅读(227) 评论(0) 推荐(0)

运算符的重载
摘要:/*运算符的重载运算必须是public和static的。公共的静态实例,而不是特定的。*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { Test2 t1 = new Test2(); t1.Count = 1; Test2 t2 = new Test2(); t2.Count = 4; System.Console.WriteLine((t1+t2).Count);//输出5 System.Consol... 阅读全文

posted @ 2013-11-27 16:55 wp456 阅读(159) 评论(0) 推荐(0)

相等性的理解
摘要:/*相等性的理解*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { /* object里面有个Equals方法和==结果一样 微软为基础结构类型和string类型重载了==运算符,所以使用==的时候比较是内容,不在是引用地址。如果自己定义类或者结果最好重写Equals和重载==运算符,这样子才不会存在比较引用地址。 object里面还有一些其他比较方法。 */ } }} 阅读全文

posted @ 2013-11-27 16:15 wp456 阅读(148) 评论(0) 推荐(0)

类型转换
摘要:/*类型转换*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { double d = 1.6; int i = (int)d;//丢失小数点后面的 System.Console.WriteLine(i);//1 double us1 = 65.1; char c1 = (char)us1; System.Console.WriteLine(c1);//A } }} 阅读全文

posted @ 2013-11-27 16:08 wp456 阅读(102) 评论(0) 推荐(0)

运算符的介绍
摘要:/*运算符的介绍*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { //检查运算 byte b = 255; checked //执行安全检查 { //b++;//异常 } unchecked//不执行安全检查,默认方式 { b++;//不异常,但是溢出会得到0 } System.Console.WriteLine(b); //is运算 int i = 10; if(i... 阅读全文

posted @ 2013-11-27 15:45 wp456 阅读(166) 评论(0) 推荐(0)

元组
摘要:/*元组元组可通过.net框架用于所有的.net语言.net 4定义了8个泛型Tuple类和一个静态的Tuple类,它们用作元组的工厂。*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { Tuple result = Divide(2,5); System.Console.WriteLine(string.Format("{0}----{1}",result.Item1,result.Item2)); } ... 阅读全文

posted @ 2013-11-27 14:41 wp456 阅读(159) 评论(0) 推荐(0)

枚举迭代
摘要:/*枚举迭代如果需要foreach迭代的支持那就要实现接口IEnumerable的GetEnumerator方法返回一个IEnumerable接口的枚举该方法通过MoveNext方法和Current属性来迭代。用yield语句轻松创建枚举器*/using System;using System.Collections.Generic;namespace Frank{ public class Test { public static void Main(string[] args) { /* IL不会把foreach解析为IL语言 C#中定义的fo... 阅读全文

posted @ 2013-11-27 14:25 wp456 阅读(309) 评论(0) 推荐(0)

数组段ArraySegment<T>的使用
摘要:/*数组段ArraySegment的使用*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { int[] array1 = new int[]{1,2,3,4,5}; int[] array2 = new int[]{6,7,8,9}; ArraySegment[] as1 = new ArraySegment[]//实例化泛型数组段,指定输入数组的开始索引及个数 { new ArraySegment(arr... 阅读全文

posted @ 2013-11-26 15:23 wp456 阅读(2465) 评论(0) 推荐(0)

数组协变和抗变
摘要:/*数组协变和抗变*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { int[] array = new int[]{1,1}; Get(array);//需要Array类型,传递的是int[]类型 协变 int[] array2 = (int[])Set();//抗变,返回Array类型,需要强制转换为int[]类型 } public static void Get(Array a)//需要Arra... 阅读全文

posted @ 2013-11-26 14:53 wp456 阅读(201) 评论(0) 推荐(0)

Array 抽象类的使用及排序
摘要:/*Array 抽象类在C#中使用[]创建数组其实是Array的表示法,C#会创建一个派生Array的新类,然后就能使用Array的方法和属性了。*/using System;namespace Frank{ public class Test { public static void Main(string[] args) { int[] ar = {1,2};//方括号C#的特殊语法,自动创建了一个派生自Array的类。 System.Console.WriteLine(ar.LongLength+"---"+ar.Rank+"---"+ar.Ge. 阅读全文

posted @ 2013-11-26 14:34 wp456 阅读(285) 评论(0) 推荐(0)

数组的定义及遍历方式
摘要:/* 数组的定义及遍历方式*/namespace Frank{ public class Test { public static void Main(string[] args) { #region 一维数组 int[] array0;//声明对象 array0 = new int[4];//定义 array0[0] = 1;//赋值 int[] array1 = new int[4];//声明并定义 array1[0] = 2;//赋值 int[] array2 = new int[1]{1};//声明定义初始化值 int[] array3 = n... 阅读全文

posted @ 2013-11-25 22:21 wp456 阅读(256) 评论(0) 推荐(0)

泛型方法
摘要:/*泛型方法泛型方法不需要定义在泛型类中泛型方法也是可以重载的,如果一个方法有泛型版本和int类型版本,那么能在编译期间确认的版本会优先调用那个类型的版本,然后再选择泛型版本。*/using System;using System.Collections.Generic;namespace Frank{ public class Test { public static void Main(string[] args) { int a = 5; Test t = new Test(); t.Get(a);//调用泛型方法,如果传的类型与定义的不一样... 阅读全文

posted @ 2013-11-25 16:48 wp456 阅读(162) 评论(0) 推荐(0)

泛型结构体
摘要:/*泛型结构体*/namespace Frank{ public class Test { public static void Main(string[] args) { } } public struct Test4 where T:struct //定义泛型结构体,跟普通的结构体没啥区别 { public Test4(T value) { this.T4 = value; } public T T4; }} 阅读全文

posted @ 2013-11-25 16:05 wp456 阅读(417) 评论(0) 推荐(0)

泛型协变和抗变
摘要:/*泛型协变和抗变.net 4.0增加的特性参数是可以协变的方法返回类型是抗变的*/namespace Frank{ public class Test { public static void Main(string[] args) { Rectangle s = new Rectangle(); Rectangle r = new Rectangle(); r.Set(s);//协边,需要基类型,但是可以传递子类型的引用,总是隐士的。 //Rectangle r2 = r.Get();//抗变,需要显示。因为返回的父类型不一定总是子类型中... 阅读全文

posted @ 2013-11-25 16:03 wp456 阅读(228) 评论(0) 推荐(0)

泛型类功能
摘要:/*----泛型---性能:泛型类型在JIT时不在进行装箱拆箱类型安全性:定义是什么类型就必须放入什么类型二进制代码重用:可以在.NET中其他语言重用代码的扩展:命名约定:在定义泛型类的时候最好使用T进行类型替代。泛型类功能默认值约束继承静态成员*/using System.Collections;using System.Collections.Generic;namespace Frank{ public class Test { public static void Main(string[] args) { } }}publi... 阅读全文

posted @ 2013-11-22 14:28 wp456 阅读(167) 评论(0) 推荐(0)

泛型---泛型链表
摘要:/*----泛型---性能:泛型类型在JIT时不在进行装箱拆箱类型安全性:定义是什么类型就必须放入什么类型二进制代码重用:可以在.NET中其他语言重用代码的扩展:命名约定:在定义泛型类的时候最好使用T进行类型替代。*/using System.Collections;using System.Collections.Generic;namespace Frank{ public class Test { public static void Main(string[] args) { List li = new List(); LinkedList l... 阅读全文

posted @ 2013-11-22 14:01 wp456 阅读(205) 评论(0) 推荐(0)

接口 interface
摘要:/*接口 interface就是一系列公共标准接口只能包含:方法、属性、索引器、事件。接口中不能定义字段,不能有构造方法。方法不能实现。不能包含运算符重载接口中的成员不能有修饰符,因为接口总是公共的。*/namespace Frank{ public class Test { public static void Main(string[] args) { } } public interface ITest2 { int Age{get;set;} void Get(); int Set(int a); } public in... 阅读全文

posted @ 2013-11-21 15:58 wp456 阅读(176) 评论(0) 推荐(0)

其他修饰符
摘要: 阅读全文

posted @ 2013-11-21 15:34 wp456 阅读(99) 评论(0) 推荐(0)

导航