当前位置: 首页>后端>正文

C# 修饰符二

淇グ绗?/h2>

abstract

  • abstract 淇グ绗?涓昏鐢ㄤ簬瀹氫箟鎶借薄绫诲拰鎶借薄鏂规硶
  • abstract 淇グ绗﹀彲鐢ㄤ簬绫汇€佹柟娉曘€佸睘鎬с€佺储寮曞拰浜嬩欢
  • 鎶借薄绫绘湰韬笉鑳借瀹炰緥鍖栵紝閫氬父鐢ㄤ簬瀹氫箟鍏锋湁鍏卞悓鐗瑰緛鐨勪竴缁勭被鐨勫熀绫伙紝鎻愪緵涓€涓彲渚涘涓淳鐢熺被鍏变韩鐨勯€氱敤鍩虹被瀹氫箟锛屾娊璞$被鍙互鍖呭惈鎶借薄鏂规硶鍜屽叿浣撴柟娉?/li>
  • 鏍囪涓?鎶借薄鐨勬垚鍛?蹇呴』鐢?娲剧敓鑷娊璞$被 鐨?闈炴娊璞$被 鏉ュ疄鐜?/li>

鎶借薄灞炴€?/h3>
  1. 鎶借薄灞炴€ф槸鍦ㄦ娊璞$被鎴栨帴鍙d腑澹版槑鐨勫睘鎬э紝鎶借薄绫诲彲浠ュ寘鍚娊璞″睘鎬у拰鍏蜂綋灞炴€э紝鑰屾帴鍙e彧鑳藉寘鍚娊璞″睘鎬?/li>
  2. 鎶借薄灞炴€ф病鏈夊叿浣撶殑瀹炵幇锛屽彧鍖呭惈灞炴€х殑澹版槑
  3. 鎶借薄灞炴€у繀椤荤敱娲剧敓绫绘彁渚涘叿浣撳疄鐜帮紝娲剧敓绫讳娇鐢?override 鍏抽敭瀛楁潵瀹炵幇鎶借薄灞炴€?/li>
  4. 娲剧敓绫诲彲浠ラ€夋嫨鎬у湴瀹炵幇鎶借薄灞炴€?
    • 娲剧敓绫绘槸闈炴娊璞$被锛屽繀椤绘彁渚涘鎶借薄灞炴€х殑瀹炵幇
    • 娲剧敓绫讳篃鏄娊璞$被锛岄€夋嫨鎬у湴灏嗘娊璞″睘鎬х暀缁欏畠鐨勬淳鐢熺被鏉ュ疄鐜?/li>
  5. 鎶借薄灞炴€т笉鑳戒娇鐢?static 鍏抽敭瀛椾慨楗帮紝鍥犱负鎶借薄灞炴€ч渶瑕佺敱鍏蜂綋鐨勫疄渚嬫彁渚涘疄鐜?/li>
  6. 鎶借薄灞炴€т笉鑳芥槸绉佹湁鐨勶紝涓嶈兘浣跨敤 private銆乸rotected 鎴?internal 绛夎闂慨楗扮锛屽洜涓哄畠浠渶瑕佸湪娲剧敓绫讳腑琚闂拰瀹炵幇
public abstract class MyAbstractClass
{
    public abstract int MyAbstractProperty { get; set; } // 鎶借薄灞炴€?
    public int MyConcreteProperty { get; set; } // 鍏蜂綋灞炴€?
}

public interface IMyInterface
{
    int MyAbstractProperty { get; set; } // 鎶借薄灞炴€?
}

public class MyDerivedClass : MyAbstractClass
{
    private int myProperty;

    public override int MyAbstractProperty
    {
        get { return myProperty; }
        set { myProperty = value; }
    }
}

鎶借薄鏂规硶

  1. 鎶借薄鏂规硶鏄湪鎶借薄绫绘垨鎺ュ彛涓0鏄庣殑鏂规硶锛屾娊璞$被鍙互鍖呭惈鎶借薄鏂规硶鍜屽叿浣撴柟娉曪紝鑰屾帴鍙e彧鑳藉寘鍚娊璞℃柟娉?/li>
  2. 鐢变簬鎶借薄鏂规硶澹版槑涓嶆彁渚涘疄闄呯殑瀹炵幇锛屽洜姝ゆ病鏈夋柟娉曚富浣擄紱鏂规硶澹版槑浠呬互鍒嗗彿缁撳熬锛屼笖绛惧悕鍚庢病鏈夊ぇ鎷彿 ({ })
  3. 鎶借薄鏂规硶蹇呴』鐢辨淳鐢熺被鎻愪緵鍏蜂綋瀹炵幇锛屾淳鐢熺被浣跨敤 override 鍏抽敭瀛楁潵瀹炵幇鎶借薄鏂规硶
  4. 娲剧敓绫诲彲浠ラ€夋嫨鎬у湴瀹炵幇鎶借薄鏂规硶
    • 娲剧敓绫绘槸闈炴娊璞$被锛屽繀椤绘彁渚涘鎶借薄鏂规硶鐨勫疄鐜?/li>
    • 娲剧敓绫讳篃鏄娊璞$被锛岄€夋嫨鎬у湴灏嗘娊璞℃柟娉曠暀缁欏畠鐨勬淳鐢熺被鏉ュ疄鐜?/li>
  5. 鎶借薄鏂规硶涓嶈兘浣跨敤 static 鍏抽敭瀛椾慨楗帮紝鍥犱负鎶借薄鏂规硶闇€瑕佺敱鍏蜂綋鐨勫疄渚嬫彁渚涘疄鐜?/li>
  6. 鎶借薄鏂规硶涓嶈兘鏄鏈夌殑锛屼笉鑳戒娇鐢?private銆乸rotected 鎴?internal 绛夎闂慨楗扮锛屽洜涓哄畠浠渶瑕佸湪娲剧敓绫讳腑琚闂拰瀹炵幇
  7. 娲剧敓绫诲彲浠ラ€夋嫨浣跨敤 new 鍏抽敭瀛楅殣钘忓熀绫荤殑鎶借薄鏂规硶锛屼絾杩欏苟涓嶆槸瀹炵幇鎶借薄鏂规硶鐨勬渶浣虫柟寮忥紝閫氬父搴旇浣跨敤 override 鍏抽敭瀛?/li>
public abstract class MyAbstractClass
{
   public abstract void MyAbstractMethod(); // 鎶借薄鏂规硶锛屾病鏈夋柟娉曚綋
   public void MyConcreteMethod() // 鍏蜂綋鏂规硶
    {
        // 鍏蜂綋瀹炵幇
    }
}

public interface IMyInterface
{
    void MyAbstractMethod(); // 鎶借薄鏂规硶
}

public class MyDerivedClass : MyAbstractClass
{
    // 1. 鎶借薄鏂规硶瀹炵幇override
    public override void MyAbstractMethod()
    {
        // 鍏蜂綋瀹炵幇
    }
   // 2. 鎶借薄鏂规硶瀹炵幇 new
    public new void MyAbstractMethod()
    {
        // 鍏蜂綋瀹炵幇锛屼絾骞朵笉鏄渶浣冲疄璺?
    }
}

鎶借薄绫?/h3>

鎶借薄绫诲叿澶囦互涓嬪姛鑳斤細

  1. 鎶借薄绫讳笉鑳藉疄渚嬪寲
  2. 鎶借薄绫诲彲浠ュ寘鍚娊璞″睘鎬с€佹娊璞℃柟娉曘€佸叿浣撴柟娉曘€佽闂櫒
  3. 鎶借薄绫?涓?鐨勫叿浣撴柟娉曪紝鍏蜂綋灞炴€т笉涓€瀹氶渶瑕佸湪娲剧敓绫讳腑杩涜瀹炵幇锛屽彲浠ユ湁鎶借薄绫绘彁渚涢粯璁ゅ疄鐜帮紝鎴栨淳鐢熺被閫夋嫨鎬ц鐩栨椿缁ф壙
  4. 涓€涓被鍖呭惈涓€涓垨澶氫釜鎶借薄鏂规硶锛岃绫诲繀椤昏澹版槑涓烘娊璞$被
  5. 褰撲竴涓被娲剧敓鑷竴涓娊璞$被骞朵笖璇ユ娊璞$被鍖呭惈鎶借薄鏂规硶鎴栧睘鎬э紙鍖呮嫭绱㈠紩鍣級鏃讹紝娲剧敓绫诲繀椤绘彁渚涘杩欎簺鎶借薄鎴愬憳鐨勫叿浣撳疄鐜帮紝鍥犱负鎶借薄鏂规硶鍜屽睘鎬ф槸娌℃湁瀹為檯瀹炵幇鐨勶紝瀹冧滑闇€瑕佸湪娲剧敓绫讳腑琚叿浣撳疄鐜?/li>
  6. 鑻ユ娊璞$被瀹炵幇浜嗘煇涓帴鍙o紝鍒欏繀椤讳负鎺ュ彛涓殑鎵€鏈夋垚鍛樻彁渚涘疄鐜帮紝鍥犱负鎺ュ彛鎴愬憳榛樿鏄娊璞$殑锛岃€屾娊璞$被涓嶈兘鍖呭惈鏈疄鐜扮殑鎶借薄鎴愬憳
  7. 鎶借薄绫诲彲浠ラ€氳繃灏嗘帴鍙f柟娉曟槧灏勫埌鎶借薄鏂规硶涓婃潵鎻愪緵鎺ュ彛鎴愬憳鐨勫疄鐜?/li>
  8. 鏃犳硶浣跨敤 sealed 淇グ绗︽潵淇敼鎶借薄绫伙紝鍥犱负涓や釜淇グ绗︾殑鍚箟鐩稿弽銆?sealed 淇グ绗﹂樆姝㈢被琚户鎵匡紝鑰?abstract 淇グ绗﹁姹傜被琚户鎵?/li>
public interface IShape
{
    double Area { get; }
    int this[int index] { get; set; }
    void Draw();
}

// 鎶借薄绫诲疄鐜版帴鍙?
public abstract class MyAbstractClass : IShape
{
    private double area;
    private int[] values = new int[3];

    // 鎶借薄灞炴€?
    public abstract string ShapeType { get; }

    // 瀹炵幇鎺ュ彛灞炴€?
    public double Area => area;
 
   // 鎶借薄绱㈠紩鍣?
    public abstract int this[int index] { get; set; }
  
  // 瀹炵幇鎺ュ彛绱㈠紩鍣?
    public int this[int index]
    {
        get { return values[index]; }
        set { values[index] = value; }
    }

    // 瀹炵幇鎺ュ彛鏂规硶
    public void Draw()
    {
        Console.WriteLine("Drawing the shape in MyAbstractClass.");
    }

    // 鍏蜂綋鏂规硶
    public void Display()
    {
        Console.WriteLine("Displaying the shape.");
    }
 
    // 鎶借薄鏂规硶锛屽繀椤荤敱娲剧敓绫绘彁渚涘叿浣撳疄鐜?
    public abstract void AbstractMethod();

    // 鎶借薄璁块棶鍣?
    public abstract string AccessorExample { get; set; }
}

// 娲剧敓绫?
public class MyDerivedClass : MyAbstractClass
{
     // 瀹炵幇鎶借薄灞炴€?
    public override string ShapeType => "Circle";

    // 鎻愪緵瀵规娊璞℃垚鍛樼殑鍏蜂綋瀹炵幇
    public override void AbstractMethod()
    {
        Console.WriteLine("Abstract method implemented in MyDerivedClass.");
    }

    // 瀹炵幇鎶借薄绱㈠紩鍣?
    public override int this[int index]
    {
        get { return index * 2; }
        set { /* 瀹炵幇绱㈠紩鍣ㄧ殑 set 鏂规硶 */ }
    }

   // 瀹炵幇鎶借薄璁块棶鍣?
    public override string AccessorExample
    {
        get { return "AccessorExample value"; }
        set { Console.WriteLine($"AccessorExample set with value: {value}"); }
    }
}

class Program
{
    static void Main()
    {
        MyDerivedClass derivedInstance = new MyDerivedClass();

        // 浣跨敤鍏蜂綋鏂规硶
        derivedInstance.Display();  // 杈撳嚭 "Displaying the shape."

        // 浣跨敤瀹炵幇鐨勬帴鍙f柟娉曘€佸睘鎬с€佺储寮曞櫒
        derivedInstance.Draw();  // 杈撳嚭 "Drawing the shape in MyAbstractClass."
        double area = derivedInstance.Area;
        Console.WriteLine($"Area: {area}");

        derivedInstance[0] = 1;
        derivedInstance[1] = 2;
        derivedInstance[2] = 3;
        Console.WriteLine($"Values: {derivedInstance[0]}, {derivedInstance[1]}, {derivedInstance[2]}");

        // 浣跨敤娲剧敓绫绘彁渚涚殑鍏蜂綋瀹炵幇
        derivedInstance.AbstractMethod();  // 杈撳嚭 "Abstract method implemented in MyDerivedClass."

        // 浣跨敤鎶借薄璁块棶鍣?
        string accessorValue = derivedInstance.AccessorExample;
        Console.WriteLine($"AccessorExample: {accessorValue}");
        derivedInstance.AccessorExample = "New Value"; // 杈撳嚭 "AccessorExample set with value: New Value"

    }
}

闂馃

瀵逛簬abstract锛屼粈涔堟儏鍐典笅缁ф壙base class锛屼粈涔堟儏鍐典笅implement interface锛孖oC澶у満鏅笅涓轰粈涔堜娇鐢╥nterface

缁ф壙 base class锛?/p>

  • 瀹氫箟鍏变韩鐨勮涓猴細褰撴湁涓€缁勭被鎷ユ湁涓€浜涘叡鍚岀殑琛屼负鎴栧睘鎬ф椂锛屽彲浠ュ皢杩欎簺鍏叡鐨勮涓哄拰灞炴€ф娊璞″嚭鏉ワ紝鏀惧埌鎶借薄绫讳腑锛屽洜涓烘娊璞$被閫氬父鐢ㄤ簬瀹氫箟鍏锋湁鍏卞悓鐗瑰緛鐨勪竴缁勭被鐨勫熀绫伙紝浣胯繖浜涚被鑳藉缁ф壙杩欎釜鎶借薄绫诲苟鍏变韩杩欎簺琛屼负
  • 鎻愪緵榛樿瀹炵幇锛?鎶借薄绫诲彲浠ュ寘鍚叿浣撴柟娉曪紝杩欎簺鏂规硶鎻愪緵浜嗛粯璁ょ殑瀹炵幇锛岃€屾淳鐢熺被鍙互閫夋嫨鎬у湴瑕嗙洊鎴栬€呯户鎵胯繖浜涘疄鐜?/li>
  • 鍏变韩鐘舵€佹垨瀛楁锛?濡傛灉涓€缁勭浉鍏崇殑绫婚渶瑕佸叡浜竴浜涚姸鎬佹垨瀛楁锛屾娊璞$被鍙互鍦ㄥ叾涓畾涔夎繖浜涚姸鎬侊紝骞舵彁渚涘杩欎簺鐘舵€佺殑璁块棶
public abstract class Animal
{
    // 鍏变韩瀛楁
    protected int speed;
    // 瀹氫箟鍏变韩鐨勮涓?
    public abstract void MakeSound();
    // 鎻愪緵榛樿瀹炵幇
    public void HasName()
    {
        Console.WriteLine("Animals have names");
    }

    public void Accelerate()
    {
        speed += 10;
    }

    public void Brake()
    {
        speed -= 10;
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Meow!");
    }
}

implement interface锛?/p>

  • 鍦ㄥ缁ф壙鐨勬儏鍐典笅锛欳# 涓竴涓被鍙兘缁ф壙鑷竴涓洿鎺ョ殑鍩虹被锛屼絾鍙互瀹炵幇澶氫釜鎺ュ彛锛屽鏋滄娊璞$被闇€瑕佸叿澶?鏌愪簺鎺ュ彛瀹氫箟 鐨勮涓猴紝鑰屽悓鏃朵篃闇€瑕佺户鎵胯嚜鍙︿竴涓熀绫伙紝閭d箞鍙互閫氳繃瀹炵幇鎺ュ彛鏉ユ弧瓒冲缁ф壙鐨勯渶姹?/li>
  • 寮鸿皟瑙勮寖锛?鎺ュ彛鎻愪緵浜嗕竴绉嶈鑼冿紝鐢ㄤ簬瀹氫箟绫诲簲璇ュ叿鏈夌殑鏂规硶銆佸睘鎬х瓑銆傞€氳繃瀹炵幇鎺ュ彛锛屾娊璞$被鍙互寮鸿皟鍏舵淳鐢熺被搴旇瀹炵幇鏌愪簺琛屼负锛屽苟纭繚绗﹀悎鐗瑰畾鐨勬帴鍙h鑼?/li>
  • 鎻愪緵閫氱敤鍔熻兘锛?鎶借薄绫诲彲浠ラ€氳繃瀹炵幇鎺ュ彛鎻愪緵涓€浜涢€氱敤鐨勫姛鑳斤紝杩欎簺鍔熻兘鍙互鍦ㄦ墍鏈夋淳鐢熺被涓叡浜€傝繖鏈夊姪浜庣‘淇濇淳鐢熺被閮芥彁渚涗簡鏌愪簺鍩烘湰鐨勮涓猴紝鍚屾椂鍏佽瀹冧滑瀹炵幇鍚勮嚜鐗瑰畾鐨勫姛鑳?/li>
public interface ILogging
{
    void LogMessage(string message);
}

public abstract class BaseClass
{
    // Common functionality for the base class
}
// 澶氱户鎵?
public abstract class LoggingBaseClass : BaseClass, ILogging
{
    public void LogMessage(string message)
    {
        // Log message implementation
    }
}

public interface IShape
{
    bool Authenticate();
    double CalculateArea();
}

public abstract class ShapeBase : IShape
{
   // 寮鸿皟瑙勮寖 鍜?鎻愪緵閫氱敤鍔熻兘
    public abstract double CalculateArea();
    public bool Authenticate()
    {
        // Authentication logic
        return true;
    }
}

IoC鍦烘櫙浣跨敤interface:

  • 渚濊禆鎬э細
    • 鍦↖oC涓娇鐢?鎺ュ彛锛屽彲浠ヨ 绫诲彧渚濊禆浜庢帴鍙e畾涔夌殑濂戠害锛岃€屼笉渚濊禆浜庡叿浣撶殑瀹炵幇
    • 鍦↖oC涓娇鐢?鎶借薄绫?锛屾剰鍛崇潃 娲剧敓绫?蹇呴』瀹炵幇鎵€鏈夌殑鎶借薄鏂规硶锛岃杩彁渚涙煇浜涢粯璁ゅ疄鐜?/li>
  • 缁ф壙鎬э細
    • 鍦↖oC涓娇鐢?鎺ュ彛锛屾帴鍙e厑璁稿涓被瀹炵幇鐩稿悓鐨勬帴鍙o紝浠庤€屽疄鐜颁簡澶氭€佹€э紝鍚屾椂涓€涓被鍙互瀹炵幇澶氫釜鎺ュ彛锛孖oC 瀹瑰櫒鑳藉鏍规嵁闇€瑕侀€夋嫨鍚堥€傜殑瀹炵幇杩涜娉ㄥ叆锛屾彁渚涗簡鏇村ぇ鐨勭伒娲绘€?/li>
    • 鎶借薄绫诲湪缁ф壙涓婃湁鍗曚竴缁ф壙鐨勯檺鍒讹紝涓€涓被鍙兘缁ф壙鑷竴涓娊璞$被銆傝繖鍦ㄤ竴浜涙儏鍐典笅鍙兘瀵艰嚧璁捐涓婄殑绾︽潫锛岀壒鍒槸褰撻渶瑕佸湪涓€涓被涓娇鐢ㄥ涓笉鍚岀殑鍔熻兘鎴栨ā鍧楁椂
  • 娴嬭瘯鎬э細
    • 閫氳繃鎺ュ彛锛屽彲浠ユ洿瀹规槗鍦拌繘琛屽崟鍏冩祴璇曘€侷oC 瀹瑰櫒鍙互浣跨敤妯℃嫙瀵硅薄鎴栬€呮祴璇曞疄鐜版潵鏇夸唬鎺ュ彛鐨勫疄闄呭疄鐜帮紝浠庤€岄殧绂诲拰娴嬭瘯绯荤粺涓殑涓嶅悓閮ㄥ垎
    • 鎶借薄绫诲彲鑳藉寘鍚叿浣撶殑瀹炵幇锛岃繖浣垮緱鍦ㄨ繘琛屽崟鍏冩祴璇曟椂闅句互浣跨敤妯℃嫙瀵硅薄銆傚湪杩涜鍗曞厓娴嬭瘯鏃讹紝鏈€濂芥槸渚濊禆浜庢帴鍙h€屼笉鏄叿浣撶殑瀹炵幇锛屼互渚胯交鏉惧湴鏇挎崲涓烘ā鎷熷璞?/li>

鎬荤殑鏉ヨ锛岃櫧鐒舵娊璞$被鍦ㄦ煇浜涙儏鍐典笅鏄湁鐢ㄧ殑锛屼絾鍦?IoC 鍦烘櫙涓紝鐢变簬涓婅堪涓€浜涢檺鍒跺拰缂洪櫡锛岄€氬父鏇村€惧悜浜庝娇鐢ㄦ帴鍙c€傛帴鍙f彁渚涗簡鏇村ぇ鐨勭伒娲绘€с€佹澗鑰﹀悎鎬у拰鍙祴璇曟€э紝鏇撮€傚悎鍦?IoC 瀹瑰櫒涓繘琛屼緷璧栨敞鍏ワ紝涔熷彲浠ユ牴鎹叿浣撻渶姹傚拰璁捐鐩爣閫夋嫨浣跨敤鎺ュ彛杩樻槸鎶借薄绫伙紝鏈夋椂鐢氳嚦涓よ€呯粨鍚堜娇鐢?/p>

abstract: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/abstract

const

  • 浣跨敤 const 鍏抽敭瀛楁潵澹版槑鏌愪釜甯搁噺瀛楁鎴栧眬閮ㄥ彉閲?/li>
  • const 瀛楁鍙兘鍦ㄨ瀛楁鐨勫0鏄庝腑鍒濆鍖?/li>
  • 甯搁噺鏄竴绉嶅湪绋嬪簭鎵ц鏈熼棿鍏跺€间笉鑳借鏀瑰彉鐨勬爣璇嗙锛屽父閲忓湪缂栬瘧鏃惰瑙f瀽锛岃€屼笉鏄湪杩愯鏃讹紝鎰忓懗鐫€甯搁噺鐨勫€煎湪绋嬪簭杩愯鍓嶅氨宸茬粡纭畾锛屽苟涓斾笉鑳藉湪杩愯鏃朵慨鏀?/li>
  • 甯搁噺閫氬父鐢ㄤ簬瀹氫箟涓嶄細鏀瑰彉鐨勫€?/li>
class Program
{
    // Pi 鍜?MaxValue 閮芥槸甯搁噺
    const double Pi = 3.14159;
    
    static void Main()
    {
        const int MaxValue = 100;
        
        Console.WriteLine($"The value of Pi is: {Pi}");
        Console.WriteLine($"The maximum value is: {MaxValue}");
    }
}

const: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/const

event

  • event 鍏抽敭瀛楃敤浜?澹版槑 鍙戝竷鏈嶅姟鍣ㄧ被涓殑浜嬩欢
  • 浜嬩欢鏄竴绉嶅湪 绫绘垨瀵硅薄 涓畾涔夌殑 鎴愬憳锛屽厑璁?鍏朵粬绫绘垨瀵硅薄 璁㈤槄锛堟垨鍙栨秷璁㈤槄锛夎浜嬩欢锛屽彲浠ュ湪浜嬩欢鍙戠敓鏃舵墽琛岀壒瀹氱殑鎿嶄綔
  • 浜嬩欢鐨勫0鏄庨€氬父鍖呭惈涓ら儴鍒嗭細浜嬩欢鐨勭被鍨嬪拰浜嬩欢澶勭悊绋嬪簭鐨勫鎵樼被鍨?/li>
  • 浜嬩欢鐨勭被鍨嬮€氬父鏄竴涓鎵樼被鍨嬶紝鐢ㄤ簬鎸囧畾浜嬩欢澶勭悊绋嬪簭鐨勬柟娉曠鍚?/li>
  • 浜嬩欢閫氬父鍦ㄧ被鍐呯殑鏌愪釜鍦版柟琚Е鍙戯紝鍗虫縺鍙戜簨浠躲€傝繖閫氬父閫氳繃璋冪敤浜嬩欢鐨勫鎵樻潵瀹屾垚
  • 浜嬩欢鏄竴绉嶇壒娈婄殑澶氭挱濮旀墭锛屼粎鍙互浠庡0鏄庝簨浠剁殑绫伙紙鎴栨淳鐢熺被锛夋垨缁撴瀯锛堝彂甯冩湇鍔″櫒绫伙級涓鍏惰繘琛岃皟鐢?/li>

涓嬪垪鍏抽敭瀛楀簲鐢ㄤ簬浜嬩欢:

C# 修饰符二,第1张
// 浜嬩欢鏁版嵁绫?
// SampleEventArgs 鏄竴涓簨浠跺弬鏁扮被锛屽寘鍚簡浜嬩欢鎼哄甫鐨勪俊鎭?
public class SampleEventArgs 
{
    public string Message { get; set; }
}

// 鍙戝竷鑰?
public class Publisher
{
    // 浜嬩欢鐨勫鐞嗙▼搴忓鎵樼被鍨?SampleEventHandler锛岀敤浜庡畾涔変簨浠跺鐞嗙▼搴忕殑鏂规硶绛惧悕
    public delegate void SampleEventHandler(object sender, SampleEventArgs e)

    // 澹版槑涓€涓猄ampleEventHandler 绫诲瀷鐨勪簨浠?
    public event SampleEventHandler SampleEvent;

    // 鍙椾繚鎶ょ殑铏氭嫙鏂规硶锛岀敤浜庤Е鍙戜簨浠?
    protected virtual void RaiseSampleEvent()
    {
        // 鍦ㄩ€傚綋鐨勬椂鍊欒Е鍙戜簨浠?
        SampleEvent?.Invoke(this, new SampleEventArgs { Message = "Event occurred!" });
    }
}

// 璁㈤槄鑰?
class Subscriber
{
    // 浜嬩欢澶勭悊绋嬪簭鏂规硶锛岀敤浜庤闃呬簨浠?
    public void HandleSampleEvent(object sender, SampleEventArgs e)
    {
        Console.WriteLine($"Received message: {e.Message}");
    }
}

class Program
{
    static void Main()
    {
        // 鍦ㄥ叾浠栫被涓垱寤?Publisher 绫荤殑瀹炰緥
        Publisher publisher = new Publisher();

        // 鍒涘缓 Subscriber 绫荤殑瀹炰緥
        Subscriber subscriber = new Subscriber();

        // 浣跨敤 += 杩愮畻绗﹁闃呬簨浠?
        publisher.SampleEvent += subscriber.HandleSampleEvent;

        // 鍦ㄩ€傚綋鐨勬椂鍊欒皟鐢?RaiseSampleEvent 鏂规硶锛岃Е鍙戜簨浠?
        publisher.RaiseSampleEvent();

        // 鍙栨秷璁㈤槄浜嬩欢
        publisher.SampleEvent -= subscriber.HandleSampleEvent; 
    }
}

// Output
// Received message: Event occurred!

event: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/event

extern

  • extern 淇グ绗︾敤浜庡0鏄庡湪澶栭儴瀹炵幇鐨勬柟娉曪紝鍦ㄥ綋鍓?C# 浠g爜涓苟鏈彁渚涙柟娉曠殑瀹炵幇锛岃€屾槸璇ユ柟娉曠殑瀹炵幇鍦ㄥ閮ㄧ殑鍏朵粬璇█涓?/li>
  • extern 淇グ绗︾殑甯歌鐢ㄦ硶鏄湪浣跨敤 Interop 鏈嶅姟璋冨叆闈炴墭绠′唬鐮佹椂涓?DllImport 鐗规€т竴璧蜂娇鐢紝鍚屾椂锛岃繕蹇呴』灏嗘柟娉曞0鏄庝负 static
  • extern 鍏抽敭瀛楄繕鍙互瀹氫箟澶栭儴绋嬪簭闆嗗埆鍚嶏紝浣垮緱鍙互浠庡崟涓▼搴忛泦涓紩鐢ㄥ悓涓€缁勪欢鐨勪笉鍚岀増鏈?/li>
//using System.Runtime.InteropServices;
class ExternTest
{
    // 绋嬪簭浣跨敤浠?User32.dll 搴撳鍏ョ殑 MessageBox 鏂规硶, 鎼厤 static 涓€璧蜂娇鐢?
    [DllImport("User32.dll", CharSet=CharSet.Unicode)]
    public static extern int MessageBox(IntPtr h, string m, string c, int type);

    static int Main()
    {
        string myString;
        Console.Write("Enter your message: ");
        myString = Console.ReadLine();
        return MessageBox((IntPtr)0, myString, "My Message Box", 0);
    }
}

extern: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/extern

new

  • new 鍏抽敭瀛楀彲浠ユ樉寮忛殣钘忎粠鍩虹被缁ф壙鐨勬垚鍛橈紝鍦ㄦ淳鐢熺被涓娇鐢?new 鍏抽敭瀛楀彲浠ラ殣钘忓熀绫讳腑鍏锋湁鐩稿悓鍚嶇О鐨勬垚鍛橈紝涓昏鐢ㄤ簬闅愯棌鍩虹被鍏变韩鍚嶇О鐨勫瓧娈点€佸睘鎬с€佹柟娉曘€佺储寮曞櫒绛夛紝浣嗚娉ㄦ剰闅愯棌鍜岃鐩?/li>
  • 绫绘垨缁撴瀯涓紩鍏ョ殑绱㈠紩鍣ㄤ細闅愯棌鍏锋湁鐩稿悓绛惧悕鐨勬墍鏈夊熀绫荤储寮曞櫒
  • new 鍏抽敭瀛楄繕鍙敤浜庡垱寤虹被鍨嬬殑瀹炰緥鎴栫敤浣滄硾鍨嬬被鍨嬬害鏉?
  • 鍚屼竴鎴愬憳鍚屾椂浣跨敤 new 鍜?override 鏄敊璇殑鍋氭硶锛屽洜涓鸿繖涓や釜淇グ绗︾殑鍚箟浜掓枼
    • new 淇グ绗︿細鐢ㄥ悓鏍风殑鍚嶇О鍒涘缓涓€涓柊鎴愬憳骞朵娇鍘熷鎴愬憳鍙樹负闅愯棌
    • override 淇グ绗︿細鎵╁睍缁ф壙鎴愬憳鐨勫疄鐜?/li>
class BaseClass
{
    public void Display()
    {
        Console.WriteLine("BaseClass Display");
    }
}

class DerivedClass : BaseClass
{
    // 1. 浣跨敤 new 鍏抽敭瀛楅殣钘忓熀绫荤殑 Display 鏂规硶
    public new void Display()
    {
        Console.WriteLine("DerivedClass Display");
    }
}

// 2. 鍒涘缓瀹炰緥锛屽疄渚嬪寲绫绘垨缁撴瀯锛屽苟涓哄叾鍒嗛厤鍐呭瓨
MyClass obj = new MyClass();

// 3. 澹版槑鍜屽垎閰嶆暟缁勶紝涓烘暟缁勫垎閰嶅唴瀛樼┖闂?
int[] numbers = new int[5];

// 4. 娉涘瀷绫诲瀷鐨勫疄渚嬪寲锛宯ew 鍏抽敭瀛椾负绫诲瀷鍙傛暟鍒涘缓鏂扮殑瀹炰緥
List<int> myList = new List<int>();

// 5. 鍦ㄦ硾鍨嬬害鏉熶腑锛宯ew() 绾︽潫琛ㄧず娉涘瀷绫诲瀷蹇呴』鍏锋湁鍏叡鐨勬棤鍙傛暟鏋勯€犲嚱鏁?
public class Example<T> where T : new()
{
    // T 蹇呴』鏈夋棤鍙傛暟鏋勯€犲嚱鏁?
}

娉ㄦ剰锛?褰?鐩稿悓鍚嶇О鐨勬柊鎴愬憳 琚0鏄庝负 涓嶅彲璋冪敤绫诲瀷 涓?鍩虹被 灏?鍏跺0鏄庝负涓€绉嶆柟娉曪紝 鍒?鏂板紩鍏ョ殑鎴愬憳涓嶄細闅愯棌鍩虹被涓殑鍚屽悕鎴愬憳

class BaseClass
{
    public void Method()
    {
        Console.WriteLine("BaseClass Method");
    }
}

class DerivedClass : BaseClass
{
    // 鍦ㄦ淳鐢熺被涓紩鍏ヤ竴涓悕涓?Method 鐨勫瓧娈?
    // 璇ュ瓧娈垫槸涓€涓鎵樼被鍨嬶紝涓嶅彲璋冪敤
    public Action Method;

    public new void Method()
    {
        Console.WriteLine("DerivedClass Method");
    }
}

class Program
{
    static void Main()
    {
        DerivedClass derivedInstance = new DerivedClass();

        // 璋冪敤鍩虹被鐨勬柟娉?
        derivedInstance.Method();  // 杈撳嚭 "BaseClass Method"

        // 璋冪敤娲剧敓绫讳腑寮曞叆鐨勫瓧娈碉紙涓嶅彲璋冪敤锛?
        derivedInstance.Method?.Invoke();  // 浠呯紪璇戦€氳繃锛屼絾瀹為檯涓婁笉浼氭墽琛屼换浣曟搷浣?
    }
}

new: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/new-modifier

override

  • override 鍏抽敭瀛楃敤浜庤〃绀轰竴涓柟娉曘€佸睘鎬с€佺储寮曞櫒鎴栦簨浠跺湪娲剧敓绫讳腑閲嶅啓鍩虹被涓殑鍚屽悕鎴愬憳
  • override 鏂规硶鎻愪緵浠庡熀绫荤户鎵跨殑鏂规硶鐨勬柊瀹炵幇锛岄€氳繃 override 澹版槑閲嶅啓鐨勬柟娉曠О涓洪噸鍐欏熀鏂规硶
    • override 鏂规硶蹇呴』鍏锋湁涓庨噸鍐欏熀鏂规硶鐩稿悓鐨勭鍚嶏紙鍚嶇О銆佽繑鍥炵被鍨嬨€佸弬鏁板垪琛ㄧ瓑锛?/li>
    • override 鏂规硶鏀寔鍗忓彉杩斿洖绫诲瀷
    • 涓嶈兘閲嶅啓闈炶櫄鏂规硶鎴栭潤鎬佹柟娉?/li>
    • 閲嶅啓鍩烘柟娉曞繀椤绘槸 virtual銆乤bstract 鎴?override
  • override 澹版槑涓嶈兘鏇存敼 virtual 鏂规硶鐨勫彲璁块棶鎬?/li>
  • 涓嶈兘浣跨敤 new銆乻tatic 鎴?virtual 淇グ绗︿慨鏀?override 鏂规硶
  • 閲嶅啓灞炴€у0鏄庡繀椤绘寚瀹氫笌缁ф壙鐨勫睘鎬у畬鍏ㄧ浉鍚岀殑璁块棶淇グ绗︺€佺被鍨嬪拰鍚嶇О
    • 鍙閲嶅啓灞炴€ф敮鎸佸崗鍙樿繑鍥炵被鍨?/li>
    • 閲嶅啓灞炴€у繀椤讳负 virtual銆乤bstract 鎴?override
class BaseClass
{
   public virtual int Value
    {
        get { return 42; }
    }

    public virtual void Display()
    {
        Console.WriteLine("BaseClass Display");
    }

    public virtual int this[int index]
    {
        get { return index * 2; }
    }

    public virtual event EventHandler MyEvent;
}

class DerivedClass : BaseClass
{
   // 1. 灞炴€х殑閲嶅啓锛氫娇鐢?override 淇グ绗﹂噸鍐欏熀绫讳腑鐨勫睘鎬?
    public override int Value
    {
        get { return 10; }
    }
    // 2. 鏂规硶鐨勯噸鍐?锛氫娇鐢?override 淇グ绗﹂噸鍐欏熀绫讳腑鐨勬柟娉?
    public override void Display()
    {
        Console.WriteLine("DerivedClass Display");
    }
   // 3. 绱㈠紩鍣ㄧ殑閲嶅啓锛氫娇鐢?override 淇グ绗﹂噸鍐欏熀绫讳腑鐨勭储寮曞櫒
    public override int this[int index]
    {
        get { return index * 10; }
    }
    // 4. 浜嬩欢鐨勯噸鍐欙細浣跨敤 override 淇グ绗﹂噸鍐欏熀绫讳腑鐨勪簨浠?
    public override event EventHandler MyEvent;
}


override: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/override

readonly

  • readonly 鍏抽敭瀛楃敤浜庡0鏄庡彧璇诲瓧娈点€傚彧璇诲瓧娈垫槸鍦ㄥ0鏄庢椂鎴栧悓涓€涓被鐨勬瀯閫犲嚱鏁颁腑琚祴鍊硷紝骞朵笖鍦ㄥ叾鐢熷懡鍛ㄦ湡鍐呬笉鍙洿鏀圭殑瀛楁锛屼竴鏃﹁祴鍊硷紝瀛楁鐨勫€煎湪涔嬪悗涓嶈兘鍐嶈淇敼

    • 鍦ㄥ0鏄庝腑鍒濆鍖栧彉閲忔椂璧嬪€?/li>
    • 鍦ㄥ寘鍚疄渚嬪瓧娈靛0鏄庣殑绫荤殑瀹炰緥鏋勯€犲嚱鏁颁腑璧嬪€?/li>
    • 鍦ㄥ寘鍚潤鎬佸瓧娈靛0鏄庣殑绫荤殑闈欐€佹瀯閫犲嚱鏁颁腑璧嬪€?/li>
    • 鏋勯€犲嚱鏁伴€€鍑哄悗锛屼笉鑳藉垎閰?readonly 瀛楁銆?姝よ鍒欏浜庡€肩被鍨嬪拰寮曠敤绫诲瀷鍏锋湁涓嶅悓鐨勫惈涔夛細
      • 鐢变簬鍊肩被鍨嬬洿鎺ュ寘鍚暟鎹紝鍥犳灞炰簬 readonly 鍊肩被鍨嬬殑瀛楁涓嶅彲鍙?/li>
      • 鐢变簬寮曠敤绫诲瀷鍖呭惈瀵瑰叾鏁版嵁鐨勫紩鐢紝鍥犳灞炰簬 readonly 寮曠敤绫诲瀷鐨勫瓧娈靛繀椤诲缁堝紩鐢ㄥ悓涓€瀵硅薄锛岃瀵硅薄鏄彲鍙樼殑锛?readonly 骞朵笉鑳介樆姝㈤€氳繃寮曠敤绫诲瀷鐨勫疄渚嬩慨鏀瑰疄渚嬪唴閮ㄧ殑鏁版嵁
  • 浣跨敤 readonly 淇グ绗︽潵澹版槑瀹炰緥鎴愬憳涓嶄細淇敼缁撴瀯鐨勭姸鎬?/p>

  • 鍦?readonly struct 绫诲瀷瀹氫箟涓紝readonly 鎸囩ず缁撴瀯绫诲瀷鏄笉鍙彉鐨?/p>

  • 鍦ㄧ粨鏋勭被鍨嬪唴鐨勫疄渚嬫垚鍛樺0鏄庝腑锛宺eadonly 鎸囩ず瀹炰緥鎴愬憳涓嶄慨鏀圭粨鏋勭殑鐘舵€?/p>

  • 鍦?ref readonly 鏂规硶杩斿洖瀹炵幇瀵瑰彧璇诲瓧娈电殑寮曠敤锛屽彧鍏佽鍦ㄥ彧璇诲瓧娈典笂鍒涘缓鍙寮曠敤锛岃€屼笉杩濆弽瀛楁鐨勫彧璇绘€?/p>

// 鍗充娇鍦ㄧ被鏋勯€犲嚱鏁颁腑缁欏瓧娈?MyReadOnlyField 璧嬩簡鍊硷紝涔熸棤娉曞湪鏂规硶 TestChange 涓洿鏀瑰叾鍊?
public class MyClass
{
    // 鍙瀛楁
    public readonly int MyReadOnlyField;

    // 鏋勯€犲嚱鏁颁腑鍒濆鍖栧彧璇诲瓧娈?
    public MyClass(int value)
    {
        MyReadOnlyField = value;
    }

    // 灏濊瘯鍦ㄥ叾浠栨柟娉曚腑淇敼鍙瀛楁锛堢紪璇戦敊璇級
    public void TestChange()
    {
        // 涓嬮潰杩欒浼氬鑷寸紪璇戦敊璇細
        // MyReadOnlyField = 42;
    }
    // 鏂规硶杩斿洖鍙瀛楁鐨勫彧璇诲紩鐢?
    public ref readonly int GetReadOnlyField()
    {
        return ref MyReadOnlyField;
    }
}
// 浣跨敤 readonly 淇グ绗︽潵澹版槑瀹炰緥鎴愬憳涓嶄細淇敼缁撴瀯鐨勭姸鎬?
public readonly double Sum()
{
    return X + Y;
}

// ref readonly
class Program
{
    static void Main()
    {
        // 鍒涘缓 MyClass 瀹炰緥
        MyClass myInstance = new MyClass(42);

        // 鑾峰彇鍙瀛楁鐨勫彧璇诲紩鐢?
        ref readonly int readOnlyRef = ref myInstance.GetReadOnlyField();

        // 閫氳繃鍙寮曠敤璁块棶鍙瀛楁
        Console.WriteLine($"Value of MyReadOnlyField: {readOnlyRef}");
    }
}

readonly 瀛楁鍙兘鍦ㄦ瀯閫犲嚱鏁颁腑杩涜璧嬪€硷紝骞朵笖鍙湁鍦ㄦ瀯閫犲嚱鏁颁笂涓嬫枃涓紝灏?readonly 瀛楁浣滀负 out 鎴?ref 鍙傛暟浼犻€掓墠鏈夋晥

public class MyClass
{
    // 鍙瀛楁
    public readonly int MyReadOnlyField;

    // 鏋勯€犲嚱鏁颁腑鍒濆鍖栧彧璇诲瓧娈?
    public MyClass(int value)
    {
        MyReadOnlyField = value;

        // 灏嗗彧璇诲瓧娈典綔涓?out 鍙傛暟浼犻€掔粰鏂规硶
        MethodWithOutParameter(out MyReadOnlyField);
    }

    // 鏂规硶鎺ユ敹鍙瀛楁浣滀负 out 鍙傛暟
    private void MethodWithOutParameter(out int value)
    {
        // 杩欓噷鍙互浣跨敤 value锛屽洜涓哄畠鏄?out 鍙傛暟
        value = 100;
    }
}
// 鍦ㄦ瀯閫犲嚱鏁颁笂涓嬫枃涓紝readonly 瀛楁鐨勮祴鍊兼槸鍏佽鐨?
// 鍦ㄥ叾浠栦笂涓嬫枃涓? 灏濊瘯淇敼 readonly 瀛楁鐨勫€煎皢瀵艰嚧缂栬瘧閿欒

**澶囨敞: **

  • readonly 鍏抽敭瀛椾笉鍚屼簬 const 鍏抽敭瀛楋紝const 瀛楁鍙兘鍦ㄨ瀛楁鐨勫0鏄庝腑鍒濆鍖?/li>
  • 鍙互鍦ㄥ瓧娈靛0鏄庡拰浠讳綍鏋勯€犲嚱鏁颁腑澶氭鍒嗛厤 readonly 瀛楁
  • 鏍规嵁鎵€浣跨敤鐨勬瀯閫犲嚱鏁帮紝readonly 瀛楁鍙兘鍏锋湁涓嶅悓鐨勫€?/li>
  • const 瀛楁鏄紪璇戞椂甯搁噺锛?readonly 瀛楁鏄彲鐢ㄤ簬杩愯鏃跺父閲?/li>
public class SamplePoint
{
    public int x;
    // Initialize a readonly field
    public readonly int y = 25;
    public readonly int z;

    public SamplePoint()
    {
        // Initialize a readonly instance field
        z = 24;
    }
    // 鏍规嵁鎵€浣跨敤鐨勬瀯閫犲嚱鏁帮紝readonly 瀛楁鍙兘鍏锋湁涓嶅悓鐨勫€?
    public SamplePoint(int p1, int p2, int p3)
    {
        x = p1;
        y = p2;
        z = p3;
    }

    public static void Main()
    {
        SamplePoint p1 = new SamplePoint(11, 21, 32);   // OK
        Console.WriteLine($"p1: x={p1.x}, y={p1.y}, z={p1.z}");
        SamplePoint p2 = new SamplePoint();
        p2.x = 55;   // OK
        Console.WriteLine($"p2: x={p2.x}, y={p2.y}, z={p2.z}");
    }
    /*
     Output:
        p1: x=11, y=21, z=32
        p2: x=55, y=25, z=24
    */
}

readonly: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/readonly

sealed

  • sealed 鍏抽敭瀛楃敤浜庨樆姝㈠叾浠栫被缁ф壙鏌愪釜绫绘垨缁撴瀯锛屾垨鑰呴樆姝㈡煇涓柟娉曡瀛愮被閲嶅啓锛岄槻姝㈡淳鐢熺被鏇夸唬鍩虹被鐨勭壒瀹氳櫄鏂规硶鎴栧睘鎬?/li>
  • 灏?abstract 淇グ绗︿笌 瀵嗗皝绫?缁撳悎浣跨敤鏄敊璇殑锛屾娊璞$被蹇呴』鐢辨彁渚涙娊璞℃柟娉曟垨灞炴€х殑瀹炵幇鐨勭被鏉ョ户鎵匡紝鑰屽瘑灏佺被 浼氶樆姝?瀛愮被鐨勯噸鍐欏拰瀹炵幇锛?/li>
  • sealed 闃绘閲嶅啓鐨勫熀绫绘柟娉曟垨灞炴€у繀椤绘槸 virtual
  • 搴旂敤浜庢柟娉曟垨灞炴€ф椂锛宻ealed 淇グ绗﹀繀椤诲缁堜笌 override 缁撳悎浣跨敤锛屽洜涓虹粨鏋勬槸闅愬紡瀵嗗皝鐨勶紝鎵€浠ユ棤娉曠户鎵垮畠浠?/li>
// 1. 闃绘绫荤殑缁ф壙 锛氶槻姝㈠叾浠栫被缁ф壙鎸囧畾鐨勭被鎴栫粨鏋?
public sealed class SealedClass
{
    // 绫荤殑鎴愬憳鍜屾柟娉?
}

// 2. 闃绘鏂规硶鐨勯噸鍐欙細闃绘瀛愮被閲嶅啓鎸囧畾鐨勮櫄鎷熸柟娉曟垨鎶借薄鏂规硶
public class BaseClass
{
    // 铏氭嫙鏂规硶
    public virtual void MyMethod()
    {
        // 瀹炵幇
    }
}

public class DerivedClass : BaseClass
{
    // 浣跨敤 sealed 闃绘閲嶅啓
    public sealed override void MyMethod()
    {
        // 瀛愮被鐨勫疄鐜?
    }
}

sealed:https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/sealed

static

  • static 淇グ绗︾敤浜庡0鏄庨潤鎬佹垚鍛橈紝闈欐€佹垚鍛樺睘浜庣被鑰屼笉鏄被鐨勫疄渚?/li>
  • static 淇グ绗﹀彲鐢ㄤ簬澹版槑 static 绫伙紝鑻?static 鍏抽敭瀛楀簲鐢ㄤ簬绫伙紝鍒欑被鐨勬墍鏈夋垚鍛橀兘蹇呴』涓?static
  • 鍙互灏?static 淇グ绗︽坊鍔犲埌瀛楁銆佹柟娉曘€佸睘鎬с€佽繍绠楃銆佷簨浠跺拰鏋勯€犲嚱鏁?/li>
  • 绫汇€佹帴鍙e拰 static 绫诲彲浠ュ叿鏈?static 鏋勯€犲嚱鏁?/li>
  • static 淇グ绗︿笉鑳界敤浜庣储寮曞櫒鎴栫粓缁撳櫒
  • static 淇グ绗︽坊鍔犲埌 鏈湴鍑芥暟锛岄潤鎬佹湰鍦板嚱鏁版棤娉曟崟鑾峰眬閮ㄥ彉閲忔垨瀹炰緥鐘舵€?/li>
  • static 淇グ绗︽坊鍔犲埌 Lambda 琛ㄨ揪寮?鎴?鍖垮悕鏂规硶锛?闈欐€丩ambda 琛ㄨ揪寮忔垨鍖垮悕鏂规硶鏃犳硶鎹曡幏灞€閮ㄥ彉閲忔垨瀹炰緥鐘舵€?/li>
  • 涓嶅彲浠ヤ娇鐢?this 寮曠敤 static 鏂规硶鎴栧睘鎬ц闂櫒
// 1. 澹版槑闈欐€佺被涓斿彧鍚潤鎬佹柟娉曪紝闈欐€佸瓧娈?
static class CompanyEmployee
{
     public static int StaticField = 42;
    public static void DoSomething() { /*...*/ }
    public static void DoSomethingElse() { /*...*/  }
}

// 2. 甯告暟鎴栫被鍨嬪0鏄庢槸闅愬紡鐨?static 鎴愬憳锛屼笉鑳介€氳繃瀹炰緥寮曠敤 static 鎴愬憳锛屼絾鍙互閫氳繃绫诲瀷鍚嶇О寮曠敤
public class MyBaseC
{
    public struct MyStruct
    {
        public static int x = 100;
    }
}
// 娉ㄦ剰锛氳寮曠敤 static 鎴愬憳 x锛岄櫎闈炲彲浠庣浉鍚岃寖鍥磋闂鎴愬憳锛屽惁鍒欒浣跨敤瀹屽叏闄愬畾鐨勫悕绉?MyBaseC.MyStruct.x
Console.WriteLine(MyBaseC.MyStruct.x);

// 3. 闈欐€佹瀯閫犲嚱鏁帮細 浣跨敤 static 澹版槑鐨勬瀯閫犲嚱鏁版槸绫荤骇鍒殑鏋勯€犲嚱鏁帮紝鍦ㄧ被琚娇鐢ㄤ箣鍓嶆墽琛屼竴娆?
public class MyClass
{
    static MyClass()
    {
        Console.WriteLine("Static constructor called.");
    }
}

// 4.闈欐€佸垵濮嬪寲
// 浣跨敤灏氭湭澹版槑鐨?static 瀛楁鏉ュ垵濮嬪寲鍙︿竴涓?static 瀛楁, 鍦ㄥ悜 static 瀛楁鏄惧紡璧嬪€间箣鍚庢墠浼氬畾涔夌粨鏋?
class Test
{
    static int x = y;
    static int y = 5;

    static void Main()
    {
        Console.WriteLine(Test.x);
        Console.WriteLine(Test.y);

        Test.x = 99;
        Console.WriteLine(Test.x);
    }
}
/*
Output:
    0
    5
    99
*/

浣跨敤闈欐€佸瓧娈靛拰鏂规硶鐨勬儏鍐典妇渚嬶細

  • 鍋囧畾姝ょ被鍖呭惈璁℃暟鍛樺伐鐨勬柟娉?AddEmployee锛堬級鍜屽瓨鍌ㄥ憳宸ヤ汉鏁扮殑瀛楁 employeeCounter
  • AddEmployee锛堬級鏂规硶鍜?employeeCounter 瀛楁鍧囦笉灞炰簬浠讳綍涓€涓憳宸ュ疄渚嬶紝 鐩稿弽锛屽畠浠睘浜庡叏浣撳憳宸ヨ繖涓被銆?搴斿皢鍏跺0鏄庝负璇ョ被鐨?static 鎴愬憳
public class Employee4
{
    public string id;
    public string name;

    public Employee4()
    {
    }

    public Employee4(string name, string id)
    {
        this.name = name;
        this.id = id;
    }

    public static int employeeCounter;

    public static int AddEmployee()
    {
        return ++employeeCounter;
    }
}

class MainClass : Employee4
{
    static void Main()
    {
        Console.Write("Enter the employee's name: ");
        string name = Console.ReadLine();
        Console.Write("Enter the employee's ID: ");
        string id = Console.ReadLine();

        // Create and configure the employee object.
        Employee4 e = new Employee4(name, id);
        Console.Write("Enter the current number of employees: ");
        string n = Console.ReadLine();
        Employee4.employeeCounter = Int32.Parse(n);
        Employee4.AddEmployee();

        // Display the new information.
        Console.WriteLine($"Name: {e.name}");
        Console.WriteLine($"ID:   {e.id}");
        Console.WriteLine($"New Number of Employees: {Employee4.employeeCounter}");
    }
}
/*
Input:
Matthias Berndt
AF643G
15
 *
Sample Output:
Enter the employee's name: Matthias Berndt
Enter the employee's ID: AF643G
Enter the current number of employees: 15
Name: Matthias Berndt
ID:   AF643G
New Number of Employees: 16
*/

static : https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/static

unsafe

  • unsafe 鍏抽敭瀛楄〃绀轰笉瀹夊叏涓婁笅鏂囷紝鍏佽鍦ㄤ笉瀹夊叏涓婁笅涓娇鐢ㄦ寚閽堝拰鐩存帴鍐呭瓨璁块棶
// 1. 鎸囬拡澹版槑鍜屼娇鐢?
unsafe
{
    int x = 10;
    int* ptr = &x;
    Console.WriteLine(*ptr); // 閫氳繃鎸囬拡璁块棶鍙橀噺鐨勫€?
}
// 2. 鍥哄畾璇彞: fixed 璇彞鐢ㄤ簬鍒涘缓鎸囧悜鎵樼鍙橀噺鐨勬寚閽堬紝浠ヤ究鍦ㄤ笉鍙楀瀮鍦惧洖鏀跺櫒骞叉壈鐨勬儏鍐典笅杩涜鍐呭瓨鎿嶄綔
unsafe
{
    fixed (int* ptr = &array[0])
    {
        // 浣跨敤 ptr 鎿嶄綔鏁扮粍
    }
}
// 3. 鎸囬拡绠楁湳
unsafe
{
    int[] array = { 1, 2, 3, 4, 5 };
    fixed (int* ptr = &array[0])
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine(*(ptr + i)); // 鎸囬拡绠楁湳
        }
    }
}
// 4. 缁撴瀯鍥哄畾
struct MyStruct
{
    public int X;
    public int Y;
}

unsafe
{
    MyStruct myStruct;
    fixed (int* ptrX = &myStruct.X, ptrY = &myStruct.Y)
    {
        // 浣跨敤鎸囬拡璁块棶缁撴瀯鐨勫瓧娈?
    }
}

unsafe:https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/unsafe

virtual

  • virtual 鍏抽敭瀛楃敤浜庝慨鏀规柟娉曘€佸睘鎬с€佺储寮曞櫒鎴栦簨浠跺0鏄庯紝骞朵娇瀹冧滑鍙互鍦ㄦ淳鐢熺被涓閲嶅啓锛屽湪闈㈠悜瀵硅薄缂栫▼涓紝杩欐槸瀹炵幇澶氭€佹€х殑涓€绉嶆柟寮?/li>
  • 铏氭嫙鎴愬憳鐨勫疄鐜板彲鐢辨淳鐢熺被涓殑鏇夸唬鎴愬憳鏇存敼
  • 璋冪敤铏氭嫙鏂规硶鏃讹紝灏嗕负鏇夸唬鐨勬垚鍛樻鏌ヨ瀵硅薄鐨勮繍琛屾椂绫诲瀷锛氫細璋冪敤澶ч儴鍒嗘淳鐢熺被涓殑璇ユ浛浠f垚鍛橈紝濡傛灉娌℃湁娲剧敓绫绘浛浠h鎴愬憳锛屽垯瀹冨彲鑳芥槸鍘熷鎴愬憳
  • 榛樿鎯呭喌涓嬶紝鏂规硶鏄潪铏氭嫙鐨勶紝涓嶈兘鏇夸唬闈炶櫄鏂规硶
  • 闈欐€佸睘鎬т笂浣跨敤 virtual 淇グ绗︽槸閿欒鐨?/li>
  • 閫氳繃鍖呮嫭浣跨敤 override 淇グ绗︾殑灞炴€у0鏄庯紝鍙湪娲剧敓绫讳腑鏇夸唬铏氭嫙缁ф壙灞炴€?/li>
public class MyBaseClass
{
    // 1. 铏氬睘鎬э細浣跨敤 virtual 鍏抽敭瀛楀0鏄庤櫄灞炴€?
    public virtual int MyProperty { get; set; }
    // 2. 铏氭柟娉曪細浣跨敤 virtual 鍏抽敭瀛楀0鏄庤櫄鏂规硶
    public virtual void MyMethod()
    {
        Console.WriteLine("Base class method");
    }
}

public class MyDerivedClass : MyBaseClass
{
    // 浣跨敤 override 鍏抽敭瀛楅噸鍐欏熀绫荤殑铏氬睘鎬?
    public override int MyProperty
    {
        get { return base.MyProperty * 2; }
        set { base.MyProperty = value; }
    }
    // 浣跨敤 override 鍏抽敭瀛楅噸鍐欏熀绫荤殑铏氭柟娉?
    public override void MyMethod()
    {
        Console.WriteLine("Derived class method");
    }
}

virtual锛歨ttps://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/virtual

volatile

  • volatile 鏄竴涓叧閿瓧锛岀敤浜庢寚绀轰竴涓瓧娈靛彲鑳戒細琚涓嚎绋嬪悓鏃惰闂紝鍥犳鍦ㄨ闂瀛楁鏃朵笉搴旇杩涜缂栬瘧鍣ㄤ紭鍖栨垨缂撳瓨
  • volatile 淇グ鐨勫瓧娈靛憡璇夌紪璇戝櫒涓嶈瀵硅繖涓瓧娈佃繘琛屼紭鍖栵紝鑰屾槸鐩存帴浠庡唴瀛樹腑璇诲彇鎴栧啓鍏ヨ繖涓瓧娈电殑鍊?/li>
  • volatile 鍏抽敭瀛楀彲搴旂敤浜庝互涓嬬被鍨嬬殑瀛楁锛?
    • 寮曠敤绫诲瀷
    • 鎸囬拡绫诲瀷锛堝湪涓嶅畨鍏ㄧ殑涓婁笅鏂囦腑锛夈€?璇锋敞鎰忥紝铏界劧鎸囬拡鏈韩鍙互鏄彲鍙樼殑锛屼絾鏄畠鎸囧悜鐨勫璞′笉鑳芥槸鍙彉鐨勩€?鎹㈠彞璇濊锛屼笉鑳藉0鏄庘€滄寚鍚戝彲鍙樺璞$殑鎸囬拡鈥?/li>
    • 绠€鍗曠被鍨嬶紝濡?sbyte銆?code>byte銆?code>short銆?code>ushort銆?code>int銆?code>uint銆?code>char銆?code>float 鍜?bool銆?/li>
    • 鍏锋湁浠ヤ笅鍩烘湰绫诲瀷涔嬩竴鐨?enum 绫诲瀷锛?code>byte銆?code>sbyte銆?code>short銆?code>ushort銆?code>int 鎴?uint銆?/li>
    • 宸茬煡涓哄紩鐢ㄧ被鍨嬬殑娉涘瀷绫诲瀷鍙傛暟
    • IntPtr 鍜?UIntPtr
  • volatile 鍏抽敭瀛楀彧鑳藉簲鐢ㄤ簬 class 鎴?struct 鐨勫瓧娈点€?涓嶈兘灏嗗眬閮ㄥ彉閲忓0鏄庝负 volatile
using System;
using System.Threading;

public class SharedResource
{
    // 浣跨敤 volatile 鍏抽敭瀛楀0鏄庡叡浜瓧娈?
    public volatile int sharedValue = 0;
}

class Program
{
    static void Main()
    {
        SharedResource sharedResource = new SharedResource();

        // 鍒涘缓涓€涓嚎绋嬩慨鏀瑰叡浜瓧娈电殑鍊?
        Thread modifyThread = new Thread(() =>
        {
            for (int i = 0; i < 10; i++)
            {
                // 淇敼鍏变韩瀛楁鐨勫€?
                sharedResource.sharedValue = i;
                Thread.Sleep(500); // 妯℃嫙鑰楁椂鎿嶄綔
            }
        });

        // 鍒涘缓鍙︿竴涓嚎绋嬭鍙栧叡浜瓧娈电殑鍊?
        Thread readThread = new Thread(() =>
        {
            while (sharedResource.sharedValue < 9)
            {
                // 璇诲彇鍏变韩瀛楁鐨勫€?
                Console.WriteLine($"Shared Value: {sharedResource.sharedValue}");
                Thread.Sleep(1000); // 妯℃嫙鑰楁椂鎿嶄綔
            }
        });

        // 鍚姩涓や釜绾跨▼
        modifyThread.Start();
        readThread.Start();

        // 绛夊緟涓や釜绾跨▼瀹屾垚
        modifyThread.Join();
        readThread.Join();
    }
}

volatile锛歨ttps://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/volatile


https://www.xamrdz.com/backend/3jt1945757.html

相关文章: