VB.NET 是否支持属性的自动 getter 和 setter?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5582836/
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
Does VB.NET support automatic getters and setters on properties?
提问by qJake
In C# I can do this:
在 C# 中,我可以这样做:
public string myProperty { get; private set; }
This is referred to as an "automatic getter/setter" (from what I've heard). Does VB.NET support these? So far, with my properties, all I can do is this:
这被称为“自动 getter/setter”(据我所知)。VB.NET 支持这些吗?到目前为止,根据我的属性,我所能做的就是:
Public Property myProperty As String
Get
Return String.Empty
End Get
Private Set(ByVal value As String)
somethingElse = value
End Set
End Property
which is extremely clunky.
这是非常笨重的。
So... is there a better way?
所以……有更好的办法吗?
回答by SLaks
Yes.
是的。
Public Property MyProperty As String
However, you can only make it ReadOnly
in VB 14 (vs 2015) or later.
但是,您只能ReadOnly
在 VB 14(vs 2015)或更高版本中制作它。
回答by pingoo
It does but only from framework 4.0 (2010)
它确实但仅来自框架 4.0 (2010)