至於我可以告訴以下兩個例子在功能方面是相同的。爲什麼VB.net和C#之間的行爲不同?
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
這是怎麼回事?爲什麼區別?
這是一個關於運行時行爲或Visual Studio智能感知的問題嗎? –
我認爲Slaks猜對了,但這個問題沒有說明問題。這也太長了。 –
@Kal_Torak它可能是,因爲我不知道爲什麼。這就是我問的原因。 –