簡體   English   中英

如何使用 C# 只讀屬性? 使用 get set 還是 lambda?

[英]How to use C# readonly property? Using get set or lambda?

namespace BrowserCompatabilityTests
{
    public static class Selectors
    {
        public static string activateDeviceButton = "#root > div > div > div > 
        div.row.activate--full-page > div:nth-child(2) > form > 
        div.form__actions.flex > button";
    }
}

我被要求將此 activateDevice 字段更新為只讀 getter 屬性。 是否有使用 {get,set} 或 lambda 的 C# 約定?

試試這個變體,它只設置一次屬性值。

public static string activateDeviceButton { get; } = "#root > div > div > div > div.row.activate--full-page > div:nth-child(2) > form > div.form__actions.flex > button";

查看 getter 和 setter 屬性。 這是它可能的樣子:

public static string activateDeviceButton { get {return value;}}

您可以使用 get(不帶 set)聲明 ReadOnly 屬性:

public string activateDeviceButton { get{ return someValue;} }

或者使用 Lambda 表達式:

   public string activateDeviceButton => someValue;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM