2

I have been watching a PluralSight video on Generics in C# (so doesnt cover this code specifically) and the example code has a constructor set out in a manner which I've never seen before:

public CircularBuffer()
    : this(capacity: 10)
{
}

public CircularBuffer(int capacity)
{
  // Other code here
}

The constructor is called like so

var cb = new CircularBuffer(capacity: 3);

What is this notation and when was it introduced? Are there any special considerations when using it? What is it there for and what benefit does it have over existing solutions?

1

1 Answer 1

9

It is named "Named Arguments" and was introduced with .NET 4.0.

It has been introduced together with optional arguments and is most relevant in this scenario. With the named argument syntax, you can provide values for some of the optional arguments without the need to supply values for the others.

No special considerations are necessary, this is strictly syntactic sugar that is used by the compiler.

Sign up to request clarification or add additional context in comments.

1 Comment

Also this is the documentation: msdn.microsoft.com/en-us/library/dd264739.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.