Static properties syntax

Viewed 40

I'm going through The C# Player’s Guide book and doing tasks. There is this one that asks to create a class for RGB color and static properties for common colors (white, black, yellow, green, blue, purple, orange). Here is the proposed solution.

For example, orange:

public static Color Orange { get; } = new Color(255,  165,    0);

I'm seeing this syntax for the first time, i.e. class name before property name (Color Orange), and constructor call after curly brackets. Where can I read more about this? I don't even know what to Google.

1 Answers

The curly braces part are called "auto properties". The're just syntax sugars for getters and setters. You can read more about it on DotNet Documentation.

The constructor part is just a initialization to the property. It's the same as if you initialized it from the constructor. But, since it's a static property without a public setter, that's the only way to initialize it.

That said, this small code is almost the same that if I write the below:

private static Color _orange = new Color(255, 165, 0);

public static GetOrange() {
  return _orange;
}
Related