什么是“价值”变量确实代表了内部类 [英] what does "value" variable really represents inside classes
问题描述
我正在阅读这篇关于在C#https://msdn.microsoft.com/en-us/library/ms379572(v = vs.80)创建二进制树的文章.aspx
I am reading this article about creating binary trees in C# https://msdn.microsoft.com/en-us/library/ms379572(v=vs.80).aspx
在一个类中我发现它们指的是一个名为"value"的变量。在此声明中"data = value;"如下: -
and inside one class i find that they are referring to a variable named "value" inside this statement "data = value;" as follow:-
public class Node<T>
{
// Private member-variables
private T data;
private NodeList<T> neighbors = null;
public Node() {}
public Node(T data) : this(data, null) {}
public Node(T data, NodeList<T> neighbors)
{
this.data = data;
this.neighbors = neighbors;
}
public T Value
{
get
{
return data;
}
set
{
data = value;
}
}
protected NodeList<T> Neighbors
{
get
{
return neighbors;
}
set
{
neighbors = value;
}
}
}
}
那么"价值"是什么?变量真的代表?因为我在类中找不到它的任何定义?
so what does the "value" variable really represents? as i can not find any definition for it inside the class?
推荐答案
这个例子有点令人困惑,因为他们使用了Value(带有大写V)属性名称。但这与'set'方法中的值(小写v)无关。
That example is a bit confusing because they used Value (with a capital V) as the property name. But this is nothing to with value (lower-case v) inside the 'set' method.
这是一个特殊的关键字,如文档
here 。
This is a special keyword as documented here.
它只存在于'set'方法中属性的简单表示正在设置的值。
It only exists inside the 'set' method of a property and simply represents the value that is being set.
示例:
public class Test
{
private int _someNumber;
public int SomeNumber
{
get
{
return _someNumber;;
}
set
{
_someNumber = value;
}
}
}
////////////////////
Test t = new Test();
t.SomeNumber = 6; // At this point the set method will be called and value will be 6
这篇关于什么是“价值”变量确实代表了内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!