C# 基础知识和VS2010的小技巧总汇(2)[转]

1、使用关键字readonly ,表示这个字段只能在执行构造函数的过程中赋值,或者由初始化语句赋值

2、.net4.0新增一个  Tuple 类,代表一个有序的N元组。可以调用Tuple.Create静态方法或使用new 关键字直接创建一个Tuple对象,.net基类库中定义了拥有1-7个泛型参数的泛型Tuple。 作用:可以使用Tuple对象作为方法的返回值。可以很容易地包含多个结果。

原文链接

public class Point
    {
        public int X { get; set; }
        public int Y { get; set; }
    }


            //the user customer data type.
             Point p = new Point() { X = 10, Y = 20 };
            //use the predefine generic tuple type.
             Tuple<int, int> p2 = new Tuple<int, int>(10, 20);
            
            //
             Console.WriteLine(p.X + p.Y);
            Console.WriteLine(p2.Item1 + p2.Item2);
//1 member
             Tuple<int> test = new Tuple<int>(1);
            //2 member ( 1< n <8 )
             Tuple<int, int> test2 = Tuple.Create<int, int>(1,2);
            //8 member , the last member must be tuple type.
             Tuple<int, int, int, int, int, int, int, Tuple<int>> test3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int>(8));

            //
             Console.WriteLine(test.Item1);
            Console.WriteLine(test2.Item1 + test2.Item2);
            Console.WriteLine(test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest.Item1);
//1 member
             Tuple<int> test = new Tuple<int>(1);
            //2 member ( 1< n <8 )
             Tuple<int, int> test2 = Tuple.Create<int, int>(1,2);
            //8 member , the last member must be tuple type.
             Tuple<int, int, int, int, int, int, int, Tuple<int>> test3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int>(8));

            //
             Console.WriteLine(test.Item1);
            Console.WriteLine(test2.Item1 + test2.Item2);
            Console.WriteLine(test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest.Item1);

3、.NET 4.0提供了一个大整数类型 BigInteger (位于System.Numerics中),这个类型可以表示任意大的整数。

4、string是引用类型。 但却可以用 "=="来比较串的内容,是因为string内部重载了==运算符。

    string类对象的加法运算是通过在内部调用string类的静态方法concat实现的。

5、类的构造函数是依附于对象的,因此一般不用它来初始化类的静态字段(或属性)。初始化类静态成员的工作由类的“静态构造函数”完成。

    类的静态构造函数只能调用一次,其调用时机为第一次访问类的静态字段时。

6、当反汇编SL程序集时,Reflector可能会弹出对话框报告 缺少程序集,这时,可以到 "Program FilesMicrosoft Silverlight版本号 或

    "Program FilesMicrosoftSDKsSilverlight版本号LibrariesClient"下去找。

7、所有的.net framework可视化窗体控件的预定义事件,都是某一对应的“事件名+Handler”委托类型的变量。与此事件相关的信息封装在“事件名+Args"类型的事件参数中,此事件参数对象派生自EventArgs.

8、在比较两个浮点数是否相等时,不能直接使用 “==”号,而必须检测两数之差。

9、使用VS自带的 ildasm 可以把.net类库反编译成IL中间语言。(位于Program FilesMicrosoft SDKsWindowsV7.0Ain)

10、使用VS20101自带的 Dotfuscator Software Services 可以保护和混淆DLL,使不能反编译,让代码不被恶意利用。

 [该功能实际上很鸡肋,需要付费才能享用更多功能]

11、所以值类型都继承自一个特殊的类ValueType

12、建议使用stringBuilder 来代替string 实现字符串连接等费性能的操作。

更多基础知识可以点击这里学习,泛型委托,lambda

原文地址:https://www.cnblogs.com/flyant/p/4305254.html