简体   繁体   English

C#创建类实例语法?

[英]C# creating class instance syntax?

I'm pretty new to coding and always used "classname instancename=new classname()" to create instances but today I've found the following code which doesn't follow and won't work with this syntax. 我对编码还很陌生,总是使用“ classname instancename = new classname()”创建实例,但是今天我发现以下代码无法使用,因此无法使用此语法。 My question is what happens in the 5th line. 我的问题是第五行会发生什么。 Thank you very much. 非常感谢你。

private void _InsertIntoBinarySearchTree(ref Element<K> p , K key)
{
    if (p == null)
    {
        p = new Element<K>();//what happens here?
        p.key = key;
    }
    else
    {
        if (p.key.CompareTo(key) > 0)
        {
            _InsertIntoBinarySearchTree(ref p.left, key);
        }
        else if (p.key.CompareTo(key) < 0)
        {
            _InsertIntoBinarySearchTree(ref p.right, key);
        }
        else
        {
            throw new Exception("");
        }
    }
}

p if by ref in the method so whatever you do it is affected outside of it; p如果通过方法中的ref进行操作,则您所做的任何事情都会在方法之外受到影响; meaning when you assign it with new the caller of the method's parameter of p is also reassigned. 这意味着当您为它分配newp ,方法参数p的调用者也会被重新分配。

You're stating it's an Element<K> type in the parameter so p must conform to that type. 您要说明它是参数中的Element<K>类型,因此p必须符合该类型。

In the method, the line in question, you reassign p to a new Element<K> via 在有问题的方法行中,您可以通过以下方式将p重新分配给new Element<K>

p = new Element<K>();//what happens here?

This is the same as p = new Element() in terms of how you're use to seeing it but since the Element type requires an generic type reference (which could be K or anything else) you need to also reassign it with the same type K passed to the method. 就如何使用它而言,它与p = new Element()相同,但是由于Element类型需要通用类型引用(可以是K或其他任何值),因此您还需要使用相同的方式重新分配它类型K传递给该方法。 Because we don't know for sure what type K is when called. 因为我们不确定何时调用K类型。

The reason you're not using var p =... or Element<K> p =.. is because p is already declared as that type in the parameter. 您不使用var p =...Element<K> p =..的原因是因为参数中已经将p声明为该类型。 It's a ref parameter so you can just reassign it as mentioned. 这是一个ref参数,因此您可以按照所述重新分配它。

The caller of the method may be like this as example only: 该方法的调用者可能仅作为示例是这样的:

Element<string> p = null;
string k = "someKey";
_InsertIntoBinarySearchTree(ref p, k); //Here p goes into the method as ref; the method performs the null check and sets p as new Element<string>()

Hope this helps... 希望这可以帮助...

First, you can't use a type specifier for the variable p since you're not declaring a new variable p , you're assigning a value to an existing variable. 首先,您不能为变量p使用类型说明符,因为您没有声明新变量p ,而是为现有变量分配了一个值。 See also: 也可以看看:

int myVal;
myVal = 5;

Second, well, you're assigning to the variable p . 其次,好吧,您要分配给变量p Thus, it'll override whatever was in there before with the new value - in this case new Element<K>() . 因此,它将使用新值(在本例中为new Element<K>()覆盖之前的任何内容。 Technically this could be any value, but since you called new , it'll create a new variable and call the appropriate constructor. 从技术上讲,它可以是任何值,但是由于您调用了new ,所以它将创建一个新变量并调用适当的构造函数。 Don't worry about memory leaks - C# will keep track of orphaned references and clean them up for you. 不必担心内存泄漏-C#将跟踪孤立的引用并为您清理它们。

Finally, since p is passed to your method as a ref , assigning to it will edit both the variable p in the local scope of the method AND the reference passed to your method from the calling scope, hence why assigning to p is useful as an output here. 最后,由于p是作为ref传递到您的方法的,因此将其赋值将在方法的本地范围内编辑变量p以及从调用范围传递给您的方法的引用,因此为什么将p用作在这里输出。

You'll notice the class of the p variable is Element<K> from the method signature on line one. 您会注意到第一行的方法签名中p变量的类是Element<K> Ignoring for the moment that it's passed by ref, it's the similar to the following code. 暂时忽略ref传递的代码,它类似于以下代码。

Element<K> p = new Element<K>();

Element is a generic class of type K. K is the type of object that is being sorted. 元素是类型K的泛型类。K是要排序的对象的类型。 It's generic because you can choose the type of element that is being sorted. 之所以通用,是因为您可以选择要排序的元素的类型。 So if you were sorting cars, for example. 因此,例如,如果您要对汽车进行分类。

Element<Car> c = new Element<Car>();

The logic of what types are allowed is defined inside the definition of the generic Element<K> class. 在通用Element<K>类的定义内定义了允许哪种类型的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM