C# ref不工作-不更改属性
我正在使用ref,这样它就改变了我试图创建的二叉搜索树的根,然而,它并没有按照我想要的方式工作C# ref不工作-不更改属性,c#,C#,我正在使用ref,这样它就改变了我试图创建的二叉搜索树的根,然而,它并没有按照我想要的方式工作 public BinaryNode<T> Root { get; set; } public BinaryTree() : base() { Root = null; public T Insert(ref BinaryNode<T> root, T val) { // Insert here
public BinaryNode<T> Root { get; set; }
public BinaryTree() : base()
{
Root = null;
public T Insert(ref BinaryNode<T> root, T val)
{
// Insert here
if (root == null)
{
BinaryNode<T> newNode = new BinaryNode<T>(val);
root = newNode;
Size++;
return val;
}
if (val.CompareTo(root.Data) < 0)
{
BinaryNode<T> left = root.LeftChild;
return Insert(ref left, val);
}
else if (val.CompareTo(root.Data) > 0)
{
BinaryNode<T> right = root.RightChild;
return Insert(ref right, val);
}
return val;
}
public override T Insert(T val)
{
BinaryNode<T> root = Root;
return Insert(ref root, val);
}
公共二进制节点根{get;set;}
public BinaryTree():base()
{
Root=null;
公共T插入(参考二进制节点根,T val)
{
//在此插入
if(root==null)
{
BinaryNode newNode=新BinaryNode(val);
根=新节点;
大小++;
返回val;
}
if(val.CompareTo(root.Data)<0)
{
BinaryNode left=root.LeftChild;
返回插入(参考左侧,val);
}
如果(val.CompareTo(root.Data)>0,则为else
{
BinaryNode right=root.RightChild;
返回插入(参考右侧,val);
}
返回val;
}
公共覆盖T插入(T val)
{
二进制节点根=根;
返回插入(参考根,val);
}
当我执行
root=newNode
时,我希望例如root在第一次插入时会发生更改。但是,事实并非如此。root即使在插入后仍保持null。我怀疑这与属性以及它如何与ref而不是ref本身交互有关?您正在修改局部根变量s因为这是您要传递的引用。如果您希望它起作用,则需要将其分配回根属性,如下所示:
public T Insert(T val)
{
BinaryNode<T> root = Root;
var result = Insert(ref root, val);
Root = root;
return result;
}
BinaryNode<T> _root;
public BinaryNode<T> Root
{
get { return _root; }
set { _root = value; }
}
public T Insert(T val)
{
return Insert(ref _root, val);
}
public T Insert(T val)
{
二进制节点根=根;
var结果=插入(参考根,val);
根=根;
返回结果;
}
也许更干净的选择是直接为属性使用支持字段,如下所示:
public T Insert(T val)
{
BinaryNode<T> root = Root;
var result = Insert(ref root, val);
Root = root;
return result;
}
BinaryNode<T> _root;
public BinaryNode<T> Root
{
get { return _root; }
set { _root = value; }
}
public T Insert(T val)
{
return Insert(ref _root, val);
}
BinaryNode\u根;
公共二进制节点根
{
获取{return\u root;}
设置{u root=value;}
}
公共T插入(T val)
{
返回插入(参考根,val);
}
您正在修改局部根变量,因为这是您要传递的引用。如果您希望它起作用,则需要将其重新分配给root属性,如下所示:
public T Insert(T val)
{
BinaryNode<T> root = Root;
var result = Insert(ref root, val);
Root = root;
return result;
}
BinaryNode<T> _root;
public BinaryNode<T> Root
{
get { return _root; }
set { _root = value; }
}
public T Insert(T val)
{
return Insert(ref _root, val);
}
public T Insert(T val)
{
二进制节点根=根;
var结果=插入(参考根,val);
根=根;
返回结果;
}
也许更干净的选择是直接为属性使用支持字段,如下所示:
public T Insert(T val)
{
BinaryNode<T> root = Root;
var result = Insert(ref root, val);
Root = root;
return result;
}
BinaryNode<T> _root;
public BinaryNode<T> Root
{
get { return _root; }
set { _root = value; }
}
public T Insert(T val)
{
return Insert(ref _root, val);
}
BinaryNode\u根;
公共二进制节点根
{
获取{return\u root;}
设置{u root=value;}
}
公共T插入(T val)
{
返回插入(参考根,val);
}
只需放置一个调试点并检查它为什么不能按预期工作,它与引用类型的工作方式有关尝试修改,使根属性不为null,因为null是未定义的引用只需放置一个调试点并检查它为什么不能按预期工作,它与引用类型的工作方式有关尝试修改,使根属性不为nullnull,因为null是未定义的referenceGood point你能解释一下为什么引用类型会出现这种情况吗?我认为为属性创建一个支持字段可以解决这个问题,但是我读到如果get/set中没有特殊的逻辑只使用auto属性。但是当使用ref试图改变nge隐藏字段。另外,赋值Root=Root
的问题是,如果我在根之后插入任何内容,就像多次插入一样,这会不会每次都改变根?这与值类型相同。属性的问题是,即使你知道你只是封装了一个隐藏字段属性基本上优于setter和getter方法,“ref”关键字需要一个变量作为引用。您可以在下面的问题中看到关于这一点的深入讨论:只有在Insert(ref BinaryNode,T)中修改“Root”变量/参数时,它才会更改Root属性方法。好的一点你能解释一下为什么引用类型的情况我认为为属性创建一个支持字段可以解决这个问题,但是我读到如果get/set中没有特殊的逻辑只使用auto属性。但是当与ref一起使用试图更改隐藏字段时,似乎存在问题赋值Root=Root
的问题是,如果我在根之后插入任何内容,比如多次插入,这会不会不只是每次都更改根?这与值类型相同。属性的问题是,即使你知道你只是封装了一个隐藏字段,属性基本上是空的-sugar over setter和getter方法以及“ref”关键字希望使用一个变量作为引用。您可以在下面的问题中看到关于这一点的深入讨论:只有在Insert(ref BinaryNode,T)方法中修改“Root”变量/参数时,才会更改Root属性。