C#技巧與解析(部分)


DesignMode

以下項目在設計器界面,需判斷DesignMode

  • OnPaint(e)/Form_Paint

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

    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 並不能反映當前環境是否是運行時,
            // 它只能告訴你這個控件當前是不是直接被設計器操作(嵌套的已經不算了) 
            bool designMode = false;
#if DEBUG
            designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime) ||
                 (Process.GetCurrentProcess().ProcessName == "devenv");
#endif
            return designMode;
        }
    }

    

override和new

  • 主要區別:

    • override重寫基類中的方法;new是隱藏基類中的方法

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

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

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

  • 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重寫
            Tiger tiger = new Tiger();
            tiger.Sound();
            tiger.Voice();
  • abstract示例:
    public abstract class Bird
    {
        public abstract void Fly();
    }

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

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

            //override重寫
            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();

可選參數

  • 可選參數必須在必備參數之后

  • 可選參數不能使用ref或out修飾符

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

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

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

params可變參數

params參數是一維數組,必須是方法中最后一個參數

        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

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

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

擴展方法

  • 聲明方法

    • 必須在一個非嵌套、非泛型的靜態類中

    • 至少有一個參數

    • 第一個參數必須附加this關鍵字作為前綴

    • 第一個參數不能有其他任何修飾符

    • 第一個參數的類型不能是指針類型

    • 第一個參數的類型稱為方法的擴展類型(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);//觸發事件
        }

            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);
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM