Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# what category does the colon " : " fall into, and what does it really mean?

Tags:

c#

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?.

like image 760
Alan Avatar asked Jun 10 '13 23:06

Alan


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is && operator in C?

The && (logical AND) operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int . Both operands must have an arithmetic or pointer type.


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 or goto label

    switch(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.

like image 168
p.s.w.g Avatar answered Oct 03 '22 23:10

p.s.w.g