• 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



  • 相关阅读:
    【概率】Uva 10900
    【组合数的唯一分解定理】Uva1635
    【欧拉定理】计算(a^(b^c))%1000000007
    【小小的思路】坑死自己的数学水题
    【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations
    【dp入门题】【跟着14练dp吧...囧】
    【补】【FZU月赛】【20150515】【待续】
    【二分查找最优解】FZU 2056 最大正方形
    【二进制】FZU 2062 Suneast & Yayamao
    【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer
  • 原文地址:https://www.cnblogs.com/zzunstu/p/9143573.html
Copyright © 2020-2023  润新知