Parameterless overloaded constructor calling parametrized one?

Hello and sorry for the confusing title, I can't express properly what I want to say. On to the question, how should I interpret this snippet?

public class Node
    public long Id { get; private set; }
    /// Creates a Node which has the supplied id.
    public Node(long id) : this()
        Id = id;
/* */ 

If I am not wrong; when a Node is created with the parametrized constructor, the this keyword should call the same method, but without parameter, which I suppose should default to the default(sic), empty constructor. At the end it looks like this is just another way of calling the empty, default constructor, which would be pointless. And if instead the constructor work as intended, how can the Id member be instantiated?

Complete sourcecode is here:


In your actual code, Node is a struct, not a class.

Adding the call to the default constructor is required in that case. Without it, you get a message saying the object "cannot be used before all of its fields are assigned to" since you're using an automatic property in a value type. This is because the compiler adds a backing field, and that field must be initialized before you can access the property. The constructor is trying to set the property before the field would be initialized. Every struct includes a default constructor, and calling it "zero-initializes" the backing field for the automatic property, allowing it to function correctly.

With a class, as you demonstrated, this would be unnecessary (and not even compile).

According to msdn:-

A constructor can invoke another constructor in the same object by using the this keyword. Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this, or as part of an expression.

When you compile the above code, it should give you an error, because you haven't defined a parameterless constructor .

Therefore, when you do this:-

  public Node(long id) : this()
      Id = id;

You are trying to call the parameterless constructor, which you haven't defined.

Need Your Help

What exactly is the libpcap format?

interface hex wireshark named-pipes libpcap

I have written a program that can make a named pipe and then write the packets to it, and later read from it using wireshark(making the named pipe as the interface -> wireshark -k -i tmp/myfifo). The

R: optimizing weights for average accross 3 variables

r optimization average weight weighted

I am trying to create an ensemble of machine learning and I have code that made predictions based on occupation Ocp, Age Age, and Gender Gender.