VB.NET 中的静态/共享和 C# 可见性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1980207/
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
static/Shared in VB.NET and C# visibility
提问by serhio
I have faced with a situation in VB.NET and C# (.NET2) with the visibility of the static/shared members. It seems to me a little strange in VB.NET:
我在 VB.NET 和 C# (.NET2) 中遇到了静态/共享成员可见的情况。在我看来,VB.NET 有点奇怪:
public class A
{
private static A instance;
public static A Instance
{
get { return instance; }
}
public string Name { get { } }
}
usage:
A.Instance.Name
// ONLY Name is "visible"
用法:
A.Instance.Name
//只有名称是“可见的”
VB.NET:
VB.NET:
Public Class A
Private Shared _instance As A
Public Shared ReadOnly Property Instance() As A
Get
Return _instance
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return ""
End Get
End Property
End Class
usage:
用法:
A.Instance.Instance.Instance.Instance...
// shared member behaves like a class public one I can repeat it to infinite..
// 共享成员的行为就像一个公共类,我可以无限重复它。
is this a Microsoft oversight or a VB.NET "feature"?
这是 Microsoft 的疏忽还是 VB.NET 的“功能”?
采纳答案by Konrad Rudolph
It's not an oversight but your VB code willtrigger a warning, which plainly means: do not use this notation.
这不是疏忽,但您的 VB 代码会触发警告,这显然意味着:不要使用此符号。
In VB, static members canbe accessed via an instance, since strictly speaking, VB doesn't have static
: VB has the keyword Shared
, meaning that the member is sharedbetween all instances, as opposed to static
where a member doesn't belong to anyinstance.
在 VB 中,静态成员可以通过实例访问,因为严格来说,VB没有static
: VB 有关键字Shared
,这意味着该成员在所有实例之间共享,而不是static
成员不属于任何实例.
Now, this is a semantical distinction between those keywords. It just so happens that these two distinct semantics tend to have the exact same effect.
现在,这是这些关键字之间的语义区别。碰巧这两种不同的语义往往具有完全相同的效果。
Of course, static
in C# is today identical to Shared
in VB.NET but their legacy is different and VB's Shared
simply has a different history and therefore historically a different meaning. With thismeaning, it makes absolutely sense to access Shared
members via an instance.
当然,static
今天在 C# 中与Shared
在 VB.NET 中相同,但它们的遗产不同,VB 的Shared
历史不同,因此在历史上具有不同的含义。有了这个意思,Shared
通过实例访问成员是绝对有意义的。
It also makes sense when used together with Option Strict Off
(loose typing): here, you sometimes don't knowa variable's type but you still might want to access a Shared
member. Now, you've got no choice but to use an instance to access it:
与Option Strict Off
(松散类型)一起使用时也有意义:在这里,您有时不知道变量的类型,但您仍然可能想要访问Shared
成员。现在,您别无选择,只能使用实例来访问它:
Option Strict Off
' … '
Dim o As Object = New A()
' Somewhere else, we want to access a static member of A but we don't know A: '
Dim instance = o.Instance
回答by David M
The C# compiler won't allow you to reference a static property on an instance of an object, only on the type itself. This is a C# rather than a .NET CLR restriction. VB.NET will allow this but will warn against it.
C# 编译器不允许您在对象的实例上引用静态属性,只能在类型本身上引用。这是 C# 而不是 .NET CLR 限制。VB.NET 将允许这样做,但会警告它。
回答by Eric Lippert
It is a feature; this is not a bug. VB is working as designed. Different languages make different choices about whether a static method can be treated as a method of an instance or not. VB allows it. C++ allows it. C# does not.
这是一个特点;这不是错误。VB 正在按设计工作。不同的语言对静态方法是否可以被视为实例的方法做出不同的选择。VB 允许。C++ 允许它。C# 没有。
Remember, the design criteria of different languages are different, and therefore the decisions made are different. On the C# design team, we highly value a language definition which makes illegal patterns that look suspicious; since there's no meaningto passing an instance as the receiver to a static method (unless computing the receiver expression causes a side effect) then why allow the user to type meaningless code?
请记住,不同语言的设计标准不同,因此做出的决定也不同。在 C# 设计团队中,我们非常重视一种使非法模式看起来可疑的语言定义;既然将实例作为接收器传递给静态方法没有意义(除非计算接收器表达式会导致副作用),那么为什么允许用户键入无意义的代码?
On the VB design team, they value the code working the way you meant it to workthe the first time you typed it; if something looks a bit dodgy, maybe give a warning, but allow it and move on.
在 VB 设计团队中,他们重视代码的工作方式,即您第一次键入它时的工作方式;如果某事看起来有点狡猾,也许会发出警告,但允许它继续前进。
If you're interested in some of the more subtle issues in the design of static calls in C#, here's an interesting issue:
如果您对 C# 中静态调用设计中的一些更微妙的问题感兴趣,这里有一个有趣的问题:
http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx
http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx