C#图解 - 扩展方法,表达式和运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    sealed class MyData
    {
        private double d1, d2, d3;

        public MyData(double d1, double d2, double d3)
        {
            this.d1 = d1;
            this.d2 = d2;
            this.d3 = d3;
        }

        public double Sum()
        {
            return d1 + d2 + d3;
        }
    }

    static class ExtendMyData
    {
        public static double Agerage(this MyData md, int count)
        {
            Console.WriteLine(count);

            return md.Sum() / count;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            MyData md = new MyData(6d,5d,9d);
            md.Agerage(3);

            Console.ReadKey();
        }
    }
}
View Code

 以上为扩展方法。

 

浅比较:
Shallow comparison,只比较引用是否相等。

深比较:
Deep Comparison,内容相等,即使引用不相等。


用户定义的类型转换:implicit和explicit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class LimitedInt
    {
        const int MaxValue = 100;
        const int MinValue = 0;

        public static implicit operator int(LimitedInt li)
        {
            return li.TheValue;
        }

        public static implicit operator LimitedInt(int x)
        {
            LimitedInt li = new LimitedInt();
            li.TheValue = x;

            return li;
        }

        private int _theValue = 0;
        public int TheValue
        {
            get
            {
                return _theValue;
            }

            set
            {
                if (value < MinValue)
                {
                    _theValue = 0;
                }
                else
                {
                    _theValue = value > MaxValue ? MaxValue : value;
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            LimitedInt li = 500;
            int k = li;

            Console.ReadKey();
        }
    }
}
View Code

 
运算符重载:
声明必须是static和public的修饰符,并且必须是要操作的类或结构的成员。重载操作符函数中的参数表是几目运算符。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class LimitedInt
    {
        const int MaxValue = 100;
        const int MinValue = 0;

        public static LimitedInt operator -(LimitedInt x)
        {
            LimitedInt li = new LimitedInt();
            li.TheValue = 0;

            return li;
        }

        public static LimitedInt operator -(LimitedInt x, LimitedInt y)
        {
            LimitedInt li = new LimitedInt();
            li.TheValue = x.TheValue - y.TheValue;

            return li;
        }

        public static LimitedInt operator +(LimitedInt x, double y)
        {
            LimitedInt li = new LimitedInt();
            li.TheValue = x.TheValue + (int)y;

            return li;
        }

        public static LimitedInt operator +(LimitedInt x, LimitedInt y)
        {
            LimitedInt li = new LimitedInt();
            li.TheValue = x.TheValue + y.TheValue;

            return li;
        }

        private int _theValue = 0;
        public int TheValue
        {
            get
            {
                return _theValue;
            }

            set
            {
                if (value < MinValue)
                {
                    _theValue = 0;
                }
                else
                {
                    _theValue = value > MaxValue ? MaxValue : value;
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            LimitedInt li1 = new LimitedInt();
            LimitedInt li2 = new LimitedInt();
            LimitedInt li3 = new LimitedInt();
            li1.TheValue = 10;
            li2.TheValue = 26;

            li3 = -li1;
            Console.WriteLine("-{0} = {1}", li1.TheValue, li3.TheValue);

            li3 = li1 - li2;
            Console.WriteLine("{0}-{1} = {2}", li1.TheValue, li2.TheValue, li3.TheValue);

            li3 = li2 + 100;
            Console.WriteLine("{0}+{1} = {2}", li2.TheValue, 100, li3.TheValue);

            li3 = li1 + li2;
            Console.WriteLine("{0}+{1} = {2}", li1.TheValue, li2.TheValue, li3.TheValue);

            Console.ReadKey();
        }
    }
}
View Code

 

typeof运算符:
GetType方法也会调用typeof运算符,该方法对每个类型的每个对象都有效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;

namespace ConsoleApplication1
{
    class SomeClass
    {
        private int _field1;
        public int field2;

        public void Method1() { }
        public int Method2() { return 2; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Type t = typeof(SomeClass);

            FieldInfo[] ts = t.GetFields();
            foreach(FieldInfo fi in ts)
            {
                Console.WriteLine(fi.Name);
            }

            MethodInfo[] ms = t.GetMethods();
            foreach (MethodInfo fi in ms)
            {
                Console.WriteLine(fi.Name);
            }

            Console.ReadKey();
        }
    }
}
View Code

 



 

转载于:https://www.cnblogs.com/zzunstu/p/9143573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值