关于C#3.0和.net一些问题的理解

1.string 和 System.String

string和System.String 是完全相同的

写下面的代码,编译之后,使用ildasm.exe查看生成的IL代码,可以发现是完全一样的

  class Program
    {
        static void Main(string[] args)
        {
            var pString = new PersonString("Xg", "Luxv");
            System.Console.WriteLine(pString);

            var pSystemString = new PersonSystemString("Xg", "Luxv");
            System.Console.WriteLine(pSystemString);

        }
    }

    internal class PersonString
    {
        public string FirstName { get; private set; }
        public string LastName { get; private set; }

        public PersonString(string FirstName, string LastName)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
        }
        public void Replace(string TextToFind, string ReplacingText)
        {
            FirstName = FirstName.Replace(TextToFind, ReplacingText);
            LastName = LastName.Replace(TextToFind, ReplacingText);
        }
        public override string ToString()
        {
            return FirstName + " " + LastName;
        }
    }
    internal class PersonSystemString
    {
        public System.String FirstName { get; private set; }
        public System.String LastName { get; private set; }

        public PersonSystemString(System.String FirstName,
            System.String LastName)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
        }
        public void Replace(System.String TextToFind,
            System.String ReplacingText)
        {
            FirstName = FirstName.Replace(TextToFind, ReplacingText);
            LastName = LastName.Replace(TextToFind, ReplacingText);
        }
        public override System.String ToString()
        {
            return FirstName + " " + LastName;
        }
    }

2.匿名类型 Var

匿名类型在第一次赋值时已经确定了类型,所以下面的代码是通不过编译的,不是可变类型,呵呵

    class Program
    {
        static void Main()
        {
            var v = "Hello World!";
            v = 10;
            System.Console.WriteLine(v);
        }
    }

3.引用类型 和 值类型

System.String虽然是引用类型(Reference Types),但他显示出来的特性很像值类型,而System.Array就不一样了,是标准的引用类型的表现

执行下面的代码:

        static void Main()
        {
            var stringValue = "Hello World!";
            var stringValue2 = stringValue;
            stringValue = "Hello Austria!";
            System.Console.WriteLine(stringValue2);


            var array = new[] { 1, 2, 3, 4 };
            var array2 = array;
            array[0] = 99;
            System.Console.WriteLine(array2[0]);
        }

结果应该是

Hello Wrold!

99

4.Finalizing Objects

像c++一样,c#的类在解构函数中释放资源是很好的方法,但这还不是足够的。看下面的代码:

    internal class FileGenerator : IDisposable
    {
        public FileGenerator()
        {
        }
        ~FileGenerator()
        {
            // Just a debug output
            Console.WriteLine("Closing file!");
        }
        public void Generate(int Length)
        {
            // Here some work is done...
        }
        public void Dispose()
        {
            // Just a debug output
            Console.WriteLine("Disposing object!");
        }
    }

    class Program
    {
        static void Generate()
        {
            using ( var fGen = new FileGenerator() )
                fGen.Generate(512);
        }
        static void Main(string[] args)
        {
            Generate();
            // Here we do some work; simulated by ReadLine statement
            Console.Write("Please Press Enter...");
            Console.ReadLine();
        }
    }

我们可以发现,"Closing file!",每次都要等到程序退出时才执行,解构函数是有.net框架的垃圾收集器调用的,什么时候调用,我们是不能控制的,所以如果需要手工控制资源释放的时候,我们需要从接口IDisposable继承一个类,然后重写Dispose(),在代码中使用using语句,这样程序将立即自动调用Dispose()的执行。

5.Nullable 类型

看下面的代码

        static void Main()
        {
            int a = 10;
            int? b = 20;
            int? c = null;
            System.Console.WriteLine(a + c ?? b);
            System.Console.Read();
        }
上面的结果是20
在c#中可以给所以的值定义Nullable类型,就是在类型前加上一个问号。两个问号??操作符 是和?配套使用的,当Nullable的值为null时,就会默认取??后面的值,
代码中c为null,所以a+c也为null,这样就会取b值,就是20

转载于:https://www.cnblogs.com/dotLive/archive/2008/02/02/1062710.html

猜你喜欢

转载自blog.csdn.net/weixin_34198881/article/details/93765136
今日推荐