c# - 为什么 VB.net 和 C# 之间的行为不同?

标签 c# vb.net

据我所知,以下两个示例在功能方面是相同的。

C#

namespace ConsoleApplication4
{
 class Program
 {
  static void Main(string[] args)
  {
   var x = new Example(12);
   var y = new Example(34);
   var z = Example.Examples.One;
  }
 }
 class Example
 {
  public static class Examples
  {
   public static readonly Example Zero = new Example(0);
   public static readonly Example One = new Example(1);
  }
  public readonly Double Value;
  public Example(Double Value)
  {
   this.Value = Value;
  }
  public static Example Add(Example x, Example y)
  {
   return new Example(x.Value + y.Value);
  }
 }
}

VB.net

Option Strict On
Module Module1

    Sub Main()

    Dim x=New Example(12)
    Dim y = New Example(34)
    Dim z=  Example.Examples.One
    End Sub

End Module

Public Class Example

  Public  Class Examples
    Public Shared ReadOnly Zero As Example
    Public Shared ReadOnly One As Example
    Public Shared ReadOnly Two As Example
    Public Shared ReadOnly MinusOne As Example
    Shared Sub new()
      Zero=New Example(0)
      One= New Example(1)
      Two = New Example(2)
      MinusOne = New Example(-1)
    End Sub
  End Class
  Public ReadOnly Value As Double
  Public Sub New(Value As Double)
    Me.Value=Value
  End Sub
  Public Shared Function Add(x As Example,y As Example) As Example
    Return New Example(x.Value+y.Value)
  End Function
End Class

那么为什么我在 C# 中只获取点后的实例方法(见下文)

z = Example.Examples.One.

在 VB.net 中

Dim z = Example.Examples.One.

我也得到了Examples

这是怎么回事?为什么不同?

最佳答案

出于兼容性原因,VB.Net 允许您通过实例限定符访问Shared (static) 方法。
不要这样做;这令人困惑。

关于c# - 为什么 VB.net 和 C# 之间的行为不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13003401/

相关文章:

javascript - Awesomium 如何单击 div 链接 - .click() 不起作用 (vb.net)

.net - Byte = 8bits,但为什么BitConverter不这么认为

vb.net - 使用 VB.NET Like Operator 与 RegEx 相比有什么优势吗?

c# - HttpClient 和设置授权 header

c# - 在循环中删除控件会导致奇怪的行为

c# - 连接列表的配置窗口

.net - VB.NET:具有公共(public) getter 和 protected setter 的属性

c# - Entity Framework ,防止重复记录,同时连接

c# - 在 MVC 2 中获取列表框的内容

c# - 如何在单独的文件中从 web.config 中提取一个部分