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;
}
}
オーバーライドして、新しいです
主な違い:
メソッドオーバーライド基本クラス書き換え、基本クラスの非表示にする新しい新しい方法
書き換え可能な仮想オーバーライド抽象修正メソッドをオーバーライドし、新しい新しい基底クラスの仮想メソッドと通常の方法では非表示にすることができます
オーバーライドは、非仮想及び静的メソッド(オーバーライドすることはできません:静的クラスを継承することはできません注意し、新しいスタティック仮想抽象メソッドのオーバーライドの変更を使用することはできません)
新しいキーワードプライベート基本クラスのメソッド隠さない派生クラス以外の唯一の基底クラスのメソッドで派生クラスに隠された変更、(そのような場合の使用禁止を、無意味)
仮想例:
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();
- 抽象例:
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();
- 例をオーバーライドします。
public class Sparrow_Black : Sparrow
{
public override void Fly()
{
Console.WriteLine(Const.BLACK);
base.Fly();
}
}
//override重写
Sparrow_Black black= new Sparrow_Black();
black.Fly();
- 新しい例:
public class Tiger_White:Tiger
{
public new void Sound()
{
Console.WriteLine();
}
}
//new
Tiger_White white = new Tiger_White();
white.Sound();
white.Voice();
オプションのパラメータ
オプションのパラメータは重要なパラメータである必要があります後
オプションのパラメータは、refまたはアウト修飾子を使用することはできません
デフォルト値は定数として指定しなければならない:数値または文字列リテラル、NULL、constメンバ、列挙部材とデフォルト(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[] { "前肢", "后肢" });
NULL可能タイプ
可能System.Nullable
int? no = null;
Console.WriteLine(no.HasValue);
Console.WriteLine(no??0);
no = 1;
Console.WriteLine(no.HasValue);
Console.WriteLine(no.Value);
拡張メソッド
METHODステートメント
あなたは、非ネストされた、非ジェネリック静的クラスでなければなりません
少なくとも一つのパラメータ
最初の引数は、接頭辞としてこのキーワードに添付しなければなりません
最初の引数は、任意の他の修飾子を持つことができません
第一のタイプはできないポインタ型パラメータであります
拡張型(拡張型)第1パラメータ法と呼ばれています
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);
}