I have been trying to get reference in the Microsoft Developer website about what the function of the : really is but I cant find it because it seems that it is neither a keyword or a operator so what is the function of the colon in C#? Also I have seen it being applied to a Method how does that function?.
-
1It's a character that is a part of expressions/statements, including the ternary operator and the case statement. What specifically are you asking about? – Kirk Woll Jun 10 '13 at 23:37
-
Possible duplicate of this? http://stackoverflow.com/questions/338398/thisfoo-syntax-in-c-sharp-constructors – Kyle Muir Jun 10 '13 at 23:38
-
1Not duplicate. That was asking about extending constructors not the purpose of `:` – FabianCook Jun 10 '13 at 23:38
-
1This question not very useful IMO. It's like asking "what is the purpose of the character 'a' in C#" and getting a list of "var", "class", etc – Blorgbeard Jun 11 '13 at 00:05
-
9@Blorgbeard Attempting to denigrate this question as being like "_what is the purpose of the character 'a'_" is utterly absurd. As per the accepted answer, `:` has ***very specific semantics*** depending on a number of possible contexts. – Disillusioned May 09 '16 at 05:50
-
So what? So does `a`. – Blorgbeard May 09 '16 at 07:15
1 Answers
Colons are used in a dozen fundamentally different places (that I can think of, with the help of everyone in the comments):
Separating a class name from its base class / interface implementations in class definitions
public class Foo : Bar { }
Specifying a generic type constraint on a generic class or method
public class Foo<T> where T : Bar { } public void Foo<T>() where T : Bar { }
Indicating how to call another constructor on the current class or a base class's constructor prior to the current constructor
public Foo() : base() { } public Foo(int bar) : this() { }
Specifying the global namespace (as C. Lang points out, this is the namespace alias qualifier)
global::System.Console
Specifying attribute targets
[assembly: AssemblyVersion("1.0.0.0")]
Specifying parameter names
Console.WriteLine(value: "Foo");
As part of a ternary expression
var result = foo ? bar : baz;
As part of a
case
orgoto
labelswitch(foo) { case bar: break; } goto Bar; Foo: return true; Bar: return false;
Since C# 6, for formatting in interpolated strings
Console.WriteLine($"{DateTime.Now:yyyyMMdd}");
Since C# 7, in tuple element names
var foo = (bar: "a", baz: "b"); Console.WriteLine(foo.bar);
In all these cases, the colon is not used as an operator or a keyword (with the exception of ::
). It falls into the category of simple syntactic symbols, like []
or {}
. They are just there to let the compiler know exactly what the other symbols around them mean.

- 69,817
- 8
- 107
- 235

- 146,324
- 30
- 291
- 331
-
1The generic type constraint answers the OP's interrogation about methods. `DoSomething
(T foo) where T : IEnumerable` – Mathieu Guindon Jun 10 '13 at 23:51 -
1In addition to calling the base constructor, you can use it to call another overload of the current class' constructor. eg: `public Foo() : this("value passed as argument") { }` – Simon Elms Jun 10 '13 at 23:53
-
@SimonTewsi / retailcoder Thanks I've included those alternate uses for completeness. They're sneaky little things--I forgot how often I used them. – p.s.w.g Jun 10 '13 at 23:59
-
So you might say that a colon is a way to qualify a type when the qualifier is too complex to fit in a single keyword, sort of how colons precede subtitles of books? – Nicholas Cousar Jun 20 '21 at 06:27