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;
            }
        }


    }
}
Zach Smith
  • 8,458
  • 13
  • 59
  • 133

7 Answers7

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.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • 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? – Zach Smith Jan 23 '17 at 11:52
  • what do you mean by `base class`? do you mean this: https://msdn.microsoft.com/en-us/library/system.type.basetype(v=vs.110).aspx ? Im newbie so please be specific in your answer :) – Amir Hossein Baghernezad Mar 28 '17 at 13:27
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.

Zach Smith
  • 8,458
  • 13
  • 59
  • 133
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • 2
    "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. – maxwellb Jul 01 '09 at 21:17
9

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

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
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.

opedog
  • 736
  • 7
  • 21
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.

vgru
  • 49,838
  • 16
  • 120
  • 201
2

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

Jeff L
  • 6,108
  • 3
  • 23
  • 29
2

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

justin.m.chase
  • 13,061
  • 8
  • 52
  • 100