VB .net 属性 C# .net 中的过程
VB .net property procedure in C# .net
在 Visual Basic .Net 中有所谓的 属性 过程
可以有以下外观
public property Name(ByVal x As String) As String
get
return name & x;
end get
set
name = x & value;
end set
end property
c# .net 中的等价物是什么。我知道 c# 具有属性和字段,但在整个 MSDN 站点上我找不到任何对 c# 属性 过程的引用。
非常感谢任何例子
我想你问的是参数化访问器(也称为参数化获取器)。 VB.NET 确实支持它们,但 C# 不支持——至少语法简洁。如果我没记错的话,CIL(CLR 的底层语言)确实支持它们,但 C# 不公开此功能(类似于 CIL 支持异常过滤器的方式 VB.NET,但 C# 直到今年才支持它们) .
因为 C# 本身不支持它们,您必须利用 C# 对匿名索引属性的支持(又名 this[T]
)来解决这个限制。这通过用一个对象替换参数化-属性 来实现,然后 表示 属性。像这样(使用你的例子):
public class Foo {
public class FooAccessor<TKey,TValue> {
private readonly Func<TKey,TValue> getter;
private readonly Action<TKey,TValue> setter;
private FooAccessor(Func<TKey,TValue> accessor, Action<TKey,TValue> mutator) {
this.getter = accessor;
this.setter = mutator;
}
public TValue this[TValue key] {
get { return this.getter( key ); }
set { this.setter( key, value ); }
}
}
private String name;
private FooAccessor<String,String> someProperty;
public FooAccessor<String,String> SomeProperty {
get {
// Lazily-initialize this.someProperty:
return this.someProperty ?? this.someProperty = new FooAccessor<String,String>(
delegate(String x) {
return this.name + x;
},
delegate(String x, String value) {
this.name = x + value;
}
);
}
}
}
因此您可以像使用已命名和参数化的对象一样使用它 属性:
Foo foo = new Foo();
// Getter
String ret = foo.SomeProperty[ "foo" ];
// Setter
foo.SomeProperty[ "bar" ] = "value";
'属性 程序有两个部分,首先是设置部分(只写),其次是获取部分(只读)。
'对我来说,Set Part Satement 就像一个子程序,而 Get part Statement 就像一个 returns 值的函数。
'首先我们必须为它的 Set Part 设置值,然后我们在 SetPart 初始化该值后从 Get Part 获取值
'属性 程序不存储任何值,所以我们声明变量来存储值。
Dim stName As String = ""
Public Property SetGetName() As String
Set(value As String)
If value = "A" Then
value += " Letter is "
End If
stName = value
End Set
Get
If stName <> "" Then
Return stName
End If
Return "Empty"
End Get
End Property
Public WriteOnly Property SetName As String
Set(value As String)
If value = "A" Then
value += " Letter is "
End If
stName = value
End Set
End Property
Public ReadOnly Property GetName As String
Get
If stName <> "" Then
Return stName
End If
Return "Empty"
End Get
End Property
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SetName = "AAA" ' here we set value
MsgBox(GetName) ' here we get value
'or
SetGetName = "AAA"
MsgBox(SetGetName)
End Sub
在 Visual Basic .Net 中有所谓的 属性 过程 可以有以下外观
public property Name(ByVal x As String) As String
get
return name & x;
end get
set
name = x & value;
end set
end property
c# .net 中的等价物是什么。我知道 c# 具有属性和字段,但在整个 MSDN 站点上我找不到任何对 c# 属性 过程的引用。
非常感谢任何例子
我想你问的是参数化访问器(也称为参数化获取器)。 VB.NET 确实支持它们,但 C# 不支持——至少语法简洁。如果我没记错的话,CIL(CLR 的底层语言)确实支持它们,但 C# 不公开此功能(类似于 CIL 支持异常过滤器的方式 VB.NET,但 C# 直到今年才支持它们) .
因为 C# 本身不支持它们,您必须利用 C# 对匿名索引属性的支持(又名 this[T]
)来解决这个限制。这通过用一个对象替换参数化-属性 来实现,然后 表示 属性。像这样(使用你的例子):
public class Foo {
public class FooAccessor<TKey,TValue> {
private readonly Func<TKey,TValue> getter;
private readonly Action<TKey,TValue> setter;
private FooAccessor(Func<TKey,TValue> accessor, Action<TKey,TValue> mutator) {
this.getter = accessor;
this.setter = mutator;
}
public TValue this[TValue key] {
get { return this.getter( key ); }
set { this.setter( key, value ); }
}
}
private String name;
private FooAccessor<String,String> someProperty;
public FooAccessor<String,String> SomeProperty {
get {
// Lazily-initialize this.someProperty:
return this.someProperty ?? this.someProperty = new FooAccessor<String,String>(
delegate(String x) {
return this.name + x;
},
delegate(String x, String value) {
this.name = x + value;
}
);
}
}
}
因此您可以像使用已命名和参数化的对象一样使用它 属性:
Foo foo = new Foo();
// Getter
String ret = foo.SomeProperty[ "foo" ];
// Setter
foo.SomeProperty[ "bar" ] = "value";
'属性 程序有两个部分,首先是设置部分(只写),其次是获取部分(只读)。 '对我来说,Set Part Satement 就像一个子程序,而 Get part Statement 就像一个 returns 值的函数。 '首先我们必须为它的 Set Part 设置值,然后我们在 SetPart 初始化该值后从 Get Part 获取值 '属性 程序不存储任何值,所以我们声明变量来存储值。
Dim stName As String = ""
Public Property SetGetName() As String
Set(value As String)
If value = "A" Then
value += " Letter is "
End If
stName = value
End Set
Get
If stName <> "" Then
Return stName
End If
Return "Empty"
End Get
End Property
Public WriteOnly Property SetName As String
Set(value As String)
If value = "A" Then
value += " Letter is "
End If
stName = value
End Set
End Property
Public ReadOnly Property GetName As String
Get
If stName <> "" Then
Return stName
End If
Return "Empty"
End Get
End Property
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SetName = "AAA" ' here we set value
MsgBox(GetName) ' here we get value
'or
SetGetName = "AAA"
MsgBox(SetGetName)
End Sub