DesignMode
The following items in the designer interface, you need to determine DesignMode
- OnPaint(e)/Form_Paint
Analyzing method requires a special custom controls, as follows:
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 and new
The main difference:
Method override base class rewriting; new new method in the base class Hide
override rewritable virtual override abstract modification method; new new base class virtual methods and ordinary methods can be hidden
override can not override a non-virtual and static methods ( Note: static class can not be inherited ), can not use the new static virtual abstract method override modification
new keywords with private modification, only hidden in the derived class in the base class methods, other than derived class does not hide base class methods ( prohibiting the use of such a case, meaningless )
virtual Examples:
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 example:
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 Example:
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 example:
public class Tiger_White:Tiger
{
public new void Sound()
{
Console.WriteLine();
}
}
//new
Tiger_White white = new Tiger_White();
white.Sound();
white.Voice();
Optional parameters
After the optional parameters must be essential parameters
Optional parameters can not use ref or out modifier
The default value must be specified as a constant: numeric or string literal, null, const member, enum member and default (T) operator
public void Move(int speed = 100)
{
Console.WriteLine("移动速度:" + speed);
}
Animal animal = new Animal();
animal.Move();
animal.Move(200);
params variable parameters
params parameter is one-dimensional array, the method must be the last parameter
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[] { "前肢", "后肢" });
Nullable type
System.Nullable
int? no = null;
Console.WriteLine(no.HasValue);
Console.WriteLine(no??0);
no = 1;
Console.WriteLine(no.HasValue);
Console.WriteLine(no.Value);
Extension Methods
Method Statement
You must be a non-nested, non-generic static class
At least one parameter
The first argument must be attached to this keyword as a prefix
The first argument can not have any other modifiers
The first type is a pointer type parameter can not
The extended type (extended type) is called a first parameter method
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());
Entrusted with the multicast delegate
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);
}