如何在 C# 中调用 getter 或 setter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15376840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I call a getter or setter in C#
提问by BIBD
I understand how to create a getters and setters
我了解如何创建 getter 和 setter
public myClass
{
public int myVal { get; set; }
// more stuff
}
but I don't understand how to call it later on.
但我不明白以后如何称呼它。
public myOtherClass
{
public myOtherClass()
{
myClass localMyClass = new myClass();
localMyClass.???set??? = 42;
// Intelisense doesn't seem to give any obvious options after I enter
// the period.
}
}
How should I set the value of myVal in localMyClass?
我应该如何在 localMyClass 中设置 myVal 的值?
采纳答案by Scott Mermelstein
localMyClass.myVal = 42;
Getters and setters let you treat the values like public properties. The difference is, you can do whatever you want inside the functions that do the getting and setting.
Getter 和 setter 使您可以将值视为公共属性。不同之处在于,您可以在执行获取和设置的函数中做任何您想做的事情。
Examples:
例子:
store other variables
存储其他变量
private int _myVal, myOtherVal;
public int MyVal { get; set { _myVal = value; myOtherVal++; } }
make numbers up / return constants
组成数字/返回常量
public int MyVal { get { return 99; } set; }
throw away the setter
扔掉二传手
private int _myVal;
public int MyVal { get { return _myVal; } set { ; } }
In each of these cases, the user will feel like it's just a public data member, and simply type
在每一种情况下,用户都会觉得它只是一个公共数据成员,只需键入
localMyClass.myVal = 42;
int i = localMyClass.myVal;
The gettors and settors let you make an implementation of your own. Also, as Hogan says, "There are a number of libraries and add-ons [e.g. MVC.NET] that require you to use getter and setter functions" - even if it's for the trivial {get; set;}
case.
gettor 和 settor 使您可以实现自己的实现。此外,正如 Hogan 所说,“有许多库和附加组件 [例如 MVC.NET] 需要您使用 getter 和 setter 函数”——即使它是针对微不足道的{get; set;}
情况。
回答by Areks
Set:
放:
localMyClass.myVal = 42
Get:
得到:
int variable = localMyClass.myVal;
回答by BinaryTox1n
Get: var tmp = localMyClass.myVal;
得到: var tmp = localMyClass.myVal;
Set: localMyClass.myVal = 2;
放: localMyClass.myVal = 2;
回答by Hogan
You want this
你要这个
localMyClass.myVal = 42;
to call the setter
调用 setter
and this
和这个
varName = localMyClass.myVal;
to call the getter.
调用吸气剂。
回答by dasblinkenlight
From the outside, the syntax for accessing getters and setters is indistinguishable from that of accessing variables. Assignments translate into calls of setters, while plain expression uses translate into calls of getters.
从外部看,访问 getter 和 setter 的语法与访问变量的语法没有区别。赋值转换为 setter 的调用,而普通表达式使用转换为 getter 的调用。
In intellisense, the list of getters and setters should open upon placing a dot .
after the variable name. Properties should have blue markers to the left of them (as opposed to magenta-colored markers to the left of methods).
在智能感知中,getter 和 setter 列表应该.
在变量名称后放置一个点时打开。属性的左边应该有蓝色标记(而不是方法左边的洋红色标记)。