C# Colon


What are the different ways you can use the colon in C#? Here I am going to briefly mention a few of them. After searching on Google, I found an article on stack overflow that states that there are a dozen different ways to use the colon. The article on stack overflow is In C# what category does the colon…. In this post, I will just mention the following examples.

  1. Inheritance
  2. Specify that a class implements an interface
  3. Specifying a generic type constraint on a generic class or method
  4. Indicating how to call another constructor on the current class or a base class’s constructor prior to the current constructor
  5. As part of a ternary expression
  6. As part of a case or goto label
  7. Since C# 6, for formatting in interpolated strings
  8. Since C# 7, tuples

1. Inheritance

The colon is used to separate the class name from its base class. Suppose the base class was Animal. The subclass is Cow. Cow inherits from Animal. Below is a very simple console example of inheritance. In Java, we would say class Cow extends Animal.

public class Animal { }
public class Cow : Animal {
    public void say() { Console.WriteLine("cow"); }
}

2. Class Implements an Interface

The colon is used to specify that the class implements an interface (or more than one interface if separated by commas). Our class CA implements the interface IInfo. In C# we use a colon but in the Java language, we use the keyword implements.

    interface IInfo
    {
        string GetName();
        string GetAge();
    }
    class CA : IInfo
    {   // declare that class CA implements the interface IInfo
        public string Name;
        public int Age;
        // implement the two interface methods:
        public string GetName() { return Name; }
        public string GetAge() { return Age.ToString(); }
    }

Generics Constraint

Generics is an “advanced” topic, although not difficult. Have a look at the post at this site called C# Generic Constraints. The colon is used to specify a generic type constraint on a generic class or method.

Constructor

Constructors in C# are a topic of study that comes early in learning about OOP in C# but they can get a bit complicated. In your code try to avoid this complexity. For a post at this site on the colon in constructors have a look at the post called C# Class Constructor Overloading. That post is part of a series of posts.

Ternary

The ternary operator is based on if-then-else. We have a post on the ternary operator called C# Ternary Operator.

Switch/Case or Goto

Below is the syntax from the w3schools.com website.

switch(expression) 
{
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
    break;
}

The goto is C# unconditional jump statement. When encountered, program flow jumps to the location specified by the goto. In the Stack Overflow post you can see an example of the use of the colon to define a label, such as Foo and Bar.

Interpolated String Formatting

Here is a post called C# String Interpolation. Below is a modified example from the Stack Overflow response to the question of how to use the colon in C#.

Console.WriteLine($"Today is {DateTime.Now:yyyyMMdd}");

Tuples

I don’t yet have a post on tuples, but you can check out Tuples In C# by C# Corner.

Leave a Reply