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(); } } }
以上为扩展方法。
浅比较:
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(); } } }
运算符重载:
声明必须是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(); } } }
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(); } } }