需要一个get和set的例子

0 c#

你能举例说明如何使用get和set吗?我需要在我的代码中调用get方法,但为了做到这一点,需要首先通过每次评估来设置它.

例如:

private static string _formatStr = Product == "test" ? "something": "other";
public static string GetFormatStr

{

get { return _formatStr; }

}
Run Code Online (Sandbox Code Playgroud)

所以我需要在每次调用get方法之前设置它.首先调用set,然后调用get.

Fre*_*els 7

所以,你的意思是你想要这个:

public static string FormatStr
{
    get
    {
         return Product == "test" ? "something" : "other";
    }
}
Run Code Online (Sandbox Code Playgroud)