C# C.net中的VB.net属性过程
在Visual Basic.Net中,有一个所谓的属性过程 哪个可以有下面的外观C# C.net中的VB.net属性过程,c#,.net,vb.net,properties,C#,.net,Vb.net,Properties,在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
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属性过程的参考
非常感谢任何示例我认为您询问的是参数化访问器,也称为参数化getter。VB.NET确实支持它们,但C不支持——至少语法简洁。如果我没记错的话,CLR的底层语言CIL确实支持它们,但C并没有像VB.NET那样公开CIL支持异常过滤器的功能,但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;
}
);
}
}
}
'属性过程有两个部分,第一个是Set部分,它是WriteOnly,第二个是Get部分,它是ReadOnly。 '对我来说,Set-Part语句就像一个子例程,Get-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
我认为C中没有与此符号完全相同的符号。如果我错了,请纠正我。我无法纠正您,因为我不知道等效符号:PWell最后一部分实际上是给其他评论者的:@downvoter为什么要对帖子进行downvote?该代码没有在VB中编译。NET@Blaatz0r我已经编辑了我的答案,以便更好地与你的原始答案相符很好的例子,在编辑之前,我在上一个答案中已经找到了,但它可能会让其他人对正在发生的事情有更多的了解。答案被接受。
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