c#专题——良构类型

1、重写GetHashCode
关于重写Equals()方法时为什么要把GetHashCode()方法一块重写,在参考博文中讲解的很好,链接如下:重写GetHashCode
2、自己做一些补充的说明:
1)就像原文中强调的那样,如果我们不使用引用类型作为dictionary,Hashtable的键的话,我们只需要重写equals方法来满足需求就好;
2)引用类型作为dictionary,Hashtable的键一定具备唯一性,当我们试图添加两个值相同的引用类型作为dictionary,Hashtable的键时,此操作应该被禁止。

  Dictionary<string, int> dc2 = new Dictionary<string, int>();


            string str1 = "1";
            string str2 = "2";
            string str3 = "2";


            dc2.Add(str1,100);
            dc2.Add(str2,200);
            dc2.Add(str3, 200);//报错,已经添加了相同键的项

2、重载操作符

 class   Coordinate
    {
        public Coordinate(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
        public int X { get; set; }
        public int Y { get; set; }


        public override bool Equals(object obj)
        {
            Coordinate coordinate = obj as Coordinate;
            if (this.X == coordinate.X && this.Y == coordinate.Y)
                return true;
            else
                return false;
        }


        public static bool operator ==(Coordinate coordinate1, Coordinate coordinate2)
        {
            if (coordinate1.X == coordinate2.X && coordinate1.Y == coordinate2.Y)
                return true;
            else
                return false;
        }
        public static bool operator !=(Coordinate coordinate1, Coordinate coordinate2)
        {
            if (coordinate1.X != coordinate2.X || coordinate1.Y == coordinate2.Y)
                return true;
            else
                return false;
        }



        public static Coordinate operator +(Coordinate coordinate1, Coordinate coordinate2)//二元操作符重载
        {
            Coordinate coordinate=new Coordinate (0,0) ;
            coordinate.X = coordinate1.X +coordinate2 .X ;
            coordinate.Y = coordinate1.Y + coordinate2.Y;
            return coordinate;
        }

        public static Coordinate operator +(Coordinate coordinate1 )//一元操作符重载
        {
            Coordinate coordinate = new Coordinate(0,0);

            coordinate.X = coordinate1.X + 100;
            coordinate.Y = coordinate1.Y + 200;
            return coordinate;

        }


        public static implicit operator double(Coordinate coordinate )//隐式转换
        {
            return coordinate.X;

        }

        public static explicit  operator int (Coordinate coordinate )//显示转换,为了防止不小心执行隐式转换
        {
            return coordinate.X ;
        }
    }

1)重载二元操作符

  Coordinate coordinate1 = new Coordinate(10,20);
            Coordinate coordinate2 = new Coordinate(10,20);
            Console.WriteLine(coordinate1 == coordinate2 ? true : false) ;
            Coordinate coordinate3 = coordinate1 + coordinate2;
            Console.WriteLine(coordinate3 .X );

2)重载一元操作符

Coordinate coordinat4 =+coordinate3;
            Console.WriteLine(coordinat4 .X );

3)隐式转换

  double hideValue = coordinat4;//隐式转换
            Console.WriteLine(hideValue);

4)显示转换

int displayValue = (int)coordinat4;//显示转换
            Console.WriteLine(displayValue );

3、垃圾回收
加入一个对象创建起来占用的内存较大,而且我们不是经常的使用他,那么我们就可以使用弱引用,如下:

自定义类

public   class Person
    {
       
        public Person(string name, double height, int age)
        {
            this.Age = age;
            this.Name = name;
            this.Height = height;
        }


        public int Age { get; set; }
        public string Name { get; set; }
        public double Height { get; set; }

    }
Person person = new Person("小王", 175, 23);
Person personNew;
        WeakReference wr1;
        public Form1()
        {
            InitializeComponent();
            wr1 = new WeakReference(new Person("", 10, 34));
            personNew= GetInstance();//获取弱引用
        }


        private Person GetInstance()
        {
            if (wr1 != null)
            {
                return wr1.Target as Person;
            }
            else
            {
                return new Person("", 10, 34);
            }
        }

4、资源清理
垃圾回收器负责内存管理工作,终结器负责释放像数据库连接,文件句柄这样的资源(参考c#本质论)
1)终结器进行自动的不确定的回收

class TemporaryFileStream
    {
        public TemporaryFileStream(string fileName)
        {
            File = new FileInfo(fileName);
            Stream = new FileStream(File.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        }

        public TemporaryFileStream() : this(Path.GetTempFileName())
        {

        }


        ~TemporaryFileStream()//终结器
        {
            Close();
        }

        public FileStream Stream { get; }
        public FileInfo File { get; }


        public void Close()
        {
            Stream?.Close();//Null操作符空值检查如果为Null则表达式短路
            File?.Delete();
        }
    }

2)using语句进行确定的终结

string fileName = “c:\1.text”;

        using (System.IO.FileStream fs = new System.IO.FileStream(fileName,System.IO.FileMode.Open ))
        {

        }

使用Using的作用就相当于使用try finally块,并且在finally块里面调用dispose方法释放资源

        string fileName = "c:\\1.text";
        System.IO.FileStream fs=default ;
        try
        {
            fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
        }
        finally
        {
           fs.Dispose ();
        }

5、延迟初始化
延迟初始化用在对象不是立即使用,而且在软件初始化加载时会明显减慢软件初始化的过程中,使用如下:
public Lazy person = new Lazy(()=>new Person (“小王”,180,34));

你可能感兴趣的:(c#)