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

猜你喜欢

转载自www.cnblogs.com/bmbh/p/11717696.html