当前位置: 爱符号 - 所有分类 - 桌面应用 - C# - C#简单重载操作符
C#简单重载操作符 C#JianDanZhongZaiCaoZuoFu
小伟 发表于:2012-10-16 16:01:00 阅读(556)
关键词:C# C# 重载 重载 操作符 操作符
摘要:C#操作符重载应用是什么呢?C#操作符重载应用的时候会有什么意外发生呢?那么本文就向你简单介绍C#操作符重载应用的情况。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Just_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Test> T = new List<Test>();
        private void button1_Click(object sender, EventArgs e)
        {
            Test T1 = new Test();
            T1.FristName = "li";
            T1.LastName = "wenwei";
            Test T2 = new Test();
            T2.FristName = "li";
            T2.LastName = "wenwei";

            T = T + T1;
            T = T + T1;
            MessageBox.Show(T.Count.ToString());
            foreach (Test test in T)
            {
                textBox1.Text += test.FristName + test.LastName + "\r\n";
            }
        }
    }
    public class Test
    {
        public string FristName;
        public string LastName;
        public static bool operator ==(Test T1, Test T2)
        {
            if (T1.FristName == T2.FristName && T1.LastName == T2.LastName)
            {
                return false;
            }
            return true;
        }
        public static bool operator !=(Test T1, Test T2)
        {
            if (T1.FristName == T2.FristName && T1.LastName == T2.LastName)
            {
                return false;
            }
            return true;
        }
        public static List<Test> operator +(List<Test> T1, Test T2)
        {
            T1.Add(T2);
            return T1;
        }
        //这个方法是要重载的
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        //这个方法是要重载的
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        //这个方法是要重载的
        public override string ToString()
        {
            return base.ToString();
        }
    }
}

声明:以上内容仅代表作者观点,不代表爱符号赞成此内容或立场