C#技巧與解析(部分)

DesignMode

以下項(xiàng)目在設(shè)計(jì)器界面,需判斷DesignMode

  • OnPaint(e)/Form_Paint

自定義控件中需要特殊方法進(jìn)行判斷,如下:

    public partial class Ctl : Control
    {
        public Ctl()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;
            g.DrawRectangle(new Pen(Brushes.Black, 5), new Rectangle(5, 5, 30, 30));

            if (!this.IsDesignMode())
                g.FillEllipse(Brushes.Red, new Rectangle(5, 5, 30, 30));
        }

        protected virtual bool IsDesignMode()
        {
            // DesignMode 并不能反映當(dāng)前環(huán)境是否是運(yùn)行時(shí),
            // 它只能告訴你這個(gè)控件當(dāng)前是不是直接被設(shè)計(jì)器操作(嵌套的已經(jīng)不算了) 
            bool designMode = false;
#if DEBUG
            designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime) ||
                 (Process.GetCurrentProcess().ProcessName == "devenv");
#endif
            return designMode;
        }
    }

    

override和new

  • 主要區(qū)別:

    • override重寫(xiě)基類中的方法;new是隱藏基類中的方法

    • override重寫(xiě)virtual override abstract修飾的方法;new可以隱藏基類中的虛方法和普通方法

    • override不能重寫(xiě)非虛方法和靜態(tài)方法(注:靜態(tài)類不能繼承),不能使用new static virtual abstract修改override方法

    • new關(guān)鍵字用private修飾,則只在派生類中隱藏了基類方法,派生類之外沒(méi)有隱藏基類方法(禁止使用這種情況,毫無(wú)意義)

  • virtual示例:

    public class Animal
    {
        public void Voice()
        {
            this.OnSound();
        }

        protected virtual void OnSound()
        {
            Console.WriteLine(Const.SOUND);
        }

    }

    public class Tiger:Animal
    {
        
        public void Sound()
        {
            base.OnSound();
        }

        protected override void OnSound()
        {
            //base.OnVoice();
            Console.WriteLine(Const.VOICE);
        }
    }

            Animal animal = new Animal();
            animal.Voice();
            //virtual重寫(xiě)
            Tiger tiger = new Tiger();
            tiger.Sound();
            tiger.Voice();
  • abstract示例:
    public abstract class Bird
    {
        public abstract void Fly();
    }

    public class Sparrow : Bird
    {
        //繼承抽象類,必須實(shí)現(xiàn)抽象類的所有方法
        public override void Fly()
        {
            Console.WriteLine(Const.FLY);
        }
    }

            //abstract重寫(xiě)
            Sparrow sparrow = new Sparrow();
            sparrow.Fly();
  • override示例:
    public class Sparrow_Black : Sparrow
    {
        public override void Fly()
        {
            Console.WriteLine(Const.BLACK);
            base.Fly();
        }
    }

            //override重寫(xiě)
            Sparrow_Black black= new Sparrow_Black();
            black.Fly();
  • new 示例:
    public class Tiger_White:Tiger
    {
        public new void Sound()
        {
            Console.WriteLine();
        }
    }

            //new
            Tiger_White white = new Tiger_White();
            white.Sound();
            white.Voice();

可選參數(shù)

  • 可選參數(shù)必須在必備參數(shù)之后

  • 可選參數(shù)不能使用ref或out修飾符

  • 指定的默認(rèn)值必須為常量:數(shù)字或字符串字面量、null、const成員、枚舉成員和default(T)操作符

        public void Move(int speed = 100)
        {
            Console.WriteLine("移動(dòng)速度:" + speed);
        }

            Animal animal = new Animal();
            animal.Move();
            animal.Move(200);

params可變參數(shù)

params參數(shù)是一維數(shù)組,必須是方法中最后一個(gè)參數(shù)

        public void Foot(params string[] foots)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var foot in foots)
                sb.Append(foot);
            Console.WriteLine(sb.ToString());
        }

            Animal animal = new Animal();
            animal.Foot("前肢");
            animal.Foot("前肢","后肢");
            animal.Foot(new string[] { "前肢", "后肢" });

可空類型

System.Nullable<T>

            int? no = null;
            Console.WriteLine(no.HasValue);
            Console.WriteLine(no??0);

            no = 1;
            Console.WriteLine(no.HasValue);
            Console.WriteLine(no.Value);

擴(kuò)展方法

  • 聲明方法

    • 必須在一個(gè)非嵌套、非泛型的靜態(tài)類中

    • 至少有一個(gè)參數(shù)

    • 第一個(gè)參數(shù)必須附加this關(guān)鍵字作為前綴

    • 第一個(gè)參數(shù)不能有其他任何修飾符

    • 第一個(gè)參數(shù)的類型不能是指針類型

    • 第一個(gè)參數(shù)的類型稱為方法的擴(kuò)展類型(extended type)

    public static class Util
    {

        public static bool IsEmpty(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return true;
            str = str.Trim();
            if (string.IsNullOrEmpty(str))
                return true;
            return false;
        }
    }

            string str = null;
            string empty = string.Empty;
            string blank = "   ";

            Console.WriteLine(str.IsEmpty());
            Console.WriteLine(empty.IsEmpty());
            Console.WriteLine(blank.IsEmpty());

委托與多播委托

        public event EventHandler Eat;//事件

        public void OnEat(EventArgs args)
        {
            if (Eat != null)
                Eat(this, args);//觸發(fā)事件
        }

            animal.Eat += Animal_Eat;//多播委托
            animal.Eat += Animal_Eat;

            animal.OnEat(new EventArgs());

        private static void Animal_Eat(object sender, EventArgs e)
        {
            Console.WriteLine(Const.EAT);
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容