August 20, 2019
Colon
In C# Language
StackOverflow – In C# what category does the colon “ : ” fall into, and what does it really mean? C# Corner – Constructor Chaining In C#
I was reading over some C# code and came across a weird use of a colon and decided to look into some of its uses to get a better understanding of when it is being used. The stack overflow answer I came across was the most inclusive and helpful for looking into it more, as it described the colon as a “simple syntactic symbols, like [] or {}” as opposed to being a type of operator.
The case I was looking at using the colon appeared to be a chain constructor setup. A colon can be used to call another constructor (or the base constructor class) prior to the current one. This led to the next link when I searched for more help on constructor chaining.
Examples:
public Foo() : base() { }public Foo(int bar) : this() { }
public class mySampleClass { public mySampleClass(): this(10) { // No parameter constructor method.// First Constructor } public mySampleClass(int Age) { // Constructor with one parameter.// Second Constructor} } }