• 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



  • 相关阅读:
    CMMI的5个级别
    ubuntu下的烧录工具
    Git 安装配置
    使用 Git & Repo 下载代码
    Git 忽略文件
    Git rebase
    使用Git进行本地提交后,未上传提交,却不小心删除了本地提交或提交所在分支,怎么办?????
    Repo
    IOS关于UIViewController之间的切换
    PresentModalViewController
  • 原文地址:https://www.cnblogs.com/zzunstu/p/9143573.html
Copyright © 2020-2023  润新知