What does this colon (:) mean?
Asked Answered
P

7

39

Before the this keyword is a colon. Can anyone explain what the colon means in this context? I don't believe this is inhertance.

Thanks

using System;

namespace LinkedListLibrary
{
    class ListNode
    {
        private object data;
        private ListNode next;

        public ListNode(object dataValue)
            : this(dataValue, null)
        {
        }

        public ListNode(object dataValue, ListNode nextNode)
        {
            data = dataValue;
            next = nextNode;
        }

        public ListNode Next
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
        public object Data
        {
            get
            {
                return data;
            }
        }


    }
}
Phocomelia answered 1/7, 2009 at 20:17 Comment(3)
See #338898Angeliaangelic
The MSDN covers the usage of the base and this keywords for constructors here: msdn.microsoft.com/en-us/library/ms173115(VS.80).aspxLoidaloin
And also see- yoda.arachsys.com/csharp/constructors.htmlWhit
H
55

It (along with the this keyword) is instructing the constructor to call another constructor within the same type before it, itself executes.

Therefore:

public ListNode(object dataValue)
    : this(dataValue, null)
{
}

effectively becomes:

public ListNode(object dataValue)
{
    data = dataValue;
    next = null;
}

Note that you can use base instead of this to instruct the constructor to call a constructor in the base class.

Hexastich answered 1/7, 2009 at 20:19 Comment(2)
7 years later.... It seems like it would be a usage of method overloading in the case of using this, is this correct? If so, can you give an example of a use-case?Sludgy
what do you mean by base class? do you mean this: msdn.microsoft.com/en-us/library/… ? Im newbie so please be specific in your answer :)Jokjakarta
T
13

It is constructor chaining so the constructor with the subsequent : this call will chain to the ctor that matches the signature.

So in this instance

public ListNode(object dataValue)

is calling

public ListNode(object dataValue, ListNode nextNode)

with null as the second param via : this(dataValue, null)

it's also worth noting that the ctor called via the colon executes before the ctor that was called to initialize the object.

Turney answered 1/7, 2009 at 20:19 Comment(1)
"ctor being called executes before": This is why in my WinForms apps, I overload the constructor, and end up with InitializeComponent() in the constructor with all of the parameters. The default constructor will call the overload, and pass default arguments. Usually I'll wind up with a chain of (0 params)->calls->(1 param)->calls->(2 params)->etc->(most params). By supplying empty ctors like "MainForm() : this( 1, null ) { }" I end up with not much overhead, and save myself from repeating code in the constructors. Bonus.Tother
C
9

It means before running the body, run the constructor with object and ListNode parameters.

Cha answered 1/7, 2009 at 20:19 Comment(0)
E
6

It calls the other ListNode constructor. You can do a similar thing with the base keyword to call a constructor of a class you're deriving from.

Excruciating answered 1/7, 2009 at 20:19 Comment(0)
E
4

No, that enables you to execute the existing constructor overload (the one with two parameters), before executing the body of the new constructor.

That's the simplest way to reuse the constructor code in multiple constructor overloads.

Eldwin answered 1/7, 2009 at 20:20 Comment(0)
B
2

The code is telling the other constructor to execute with the supplied arguments before the body of the current constructor is executed.

Bannister answered 1/7, 2009 at 20:24 Comment(4)
I think you're thinking of C++. This is C#.Airwaves
Doh. That's what happens when my glance at the tags is just a bit too quick.Bannister
Might I suggest either editing out the C++ part (which is still somewhat confusing to a C# developer) or deleting the answer?Airwaves
Removed the part about C++ initialization lists.Bannister
C
2

Constructor chain arguments. There is also ": base()" for chaining a call to a constructor on the base type.

Cysto answered 1/7, 2009 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.