简体   繁体   中英

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. 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; meaning when you assign it with new the caller of the method's parameter of p is also reassigned.

You're stating it's an Element<K> type in the parameter so p must conform to that type.

In the method, the line in question, you reassign p to a new Element<K> via

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. Because we don't know for sure what type K is when called.

The reason you're not using var p =... or Element<K> p =.. is because p is already declared as that type in the parameter. It's a ref parameter so you can just reassign it as mentioned.

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. See also:

int myVal;
myVal = 5;

Second, well, you're assigning to the variable p . Thus, it'll override whatever was in there before with the new value - in this case 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. Don't worry about memory leaks - C# will keep track of orphaned references and clean them up for you.

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.

You'll notice the class of the p variable is Element<K> from the method signature on line one. Ignoring for the moment that it's passed by ref, it's the similar to the following code.

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

Element is a generic class of type K. K is the type of object that is being sorted. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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