简体   繁体   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";
    }
}

I was asked to update this activateDevice field to a readonly getter property.我被要求将此 activateDevice 字段更新为只读 getter 属性。 Is there a C# convention to use {get,set} or lambda?是否有使用 {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";

Look into getter and setter properties.查看 getter 和 setter 属性。 Here is how it would potentially look:这是它可能的样子:

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

You may declare ReadOnly properties either with a get (without set):您可以使用 get(不带 set)声明 ReadOnly 属性:

public string activateDeviceButton { get{ return someValue;} }

Or with a Lambda Expression:或者使用 Lambda 表达式:

   public string activateDeviceButton => someValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM