Some Important Keywords of C#: Part 1

This is a very simple Part 1 article but will clarify some of the concepts of C# for those who are learning or for those also who might have good knowledge but are unfortunately not yet familiar with these important features.

as

The as keyword is pretty cool, it also shows a very cool behavior.

The first of the following examples will return null if the object isn't a Mango, rather than throwing a class cast exception that will be the result of the second line.

Mango a = fruit as Mango; // 'as' keyword returns null if fruit is NOT type of Mango
Mango m = (Mango)fruit; // This line will throw an error

Example

public class Fruit
{
    // Some Code here
}

public class Mango : Fruit
{
    // Some Code here
}

public class Apple : Fruit
{
    // Some Code here
}

class Program
{
    static void Main(string[] args)
    {
        Fruit fruit = new Fruit();
        Mango a = fruit as Mango; // 'as' key will return null if fruit is NOT type of Mango
        Mango m = (Mango)fruit; // This Line will throw an error
        Console.WriteLine(a);
        Console.ReadLine(); 
    }
}

default

The default keyword is a generic type, as in the following.

T t = default(T);

The output will be a "null" if T is a reference type, 0 if it is an int, false if it is a Boolean, and so on.

yield

The use of the yield keyword in any statement indicates that the method, operator, or accessor that is to use it is an iterator.

Here is a very simple example that explains how a method can behave like an iterator.

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<int> x = RetValue();

        foreach (var item1 in x)
        {
            Console.WriteLine(item1);
        }

        IEnumerable<int> y = RetValue2();

        foreach (var item1 in y)
        {
            Console.WriteLine(item1);
        }

        Console.ReadLine();
    }

    public static IEnumerable<int> RetValue()
    {
        for (int i = 0; i < 10; i++)
        {
            if (i == 5)
                yield break;
            else
                yield return i;
        }
    }

    public static IEnumerable<int> RetValue2()
    {
        for (int i = 0; i < 10; i++)
        {
            if (i == 5)
                yield break;
            else
                yield return i * i - 1;
        }
    }
}

Output: 0,1,2,3,4

-1 0 3 8 15

Tuple

A tuple is a class that can have many items of various types. You can have a value type such as an int or reference type as a string in a tuple.

// Create three-item tuple.
Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(1, "horse", true);
// Access tuple properties.
if (tuple.Item1 == 1)
    Console.WriteLine(tuple.Item1);
if (tuple.Item2 == "horse")
    Console.WriteLine(tuple.Item2);
if (tuple.Item3)
    Console.WriteLine(tuple.Item3);

// Passing Tuple as argument Example
static void Main()
{
    // Create four-item tuple; use var implicit type.
    var tuple = new Tuple<string, string[], int, int[]>("Python",
        new string[] { "TypeScript", "c#" },
        1,
        new int[] { 2, 3 });
    // tuple as argument.
    MyTuples(tuple);
}

static void MyTuples(Tuple<string, string[], int, int[]> tuple)
{
    // Evaluate the tuple's items.
    Console.WriteLine(tuple.Item1);
    foreach (string value in tuple.Item2)
        Console.WriteLine(value);
    Console.WriteLine(tuple.Item3);
    foreach (int value in tuple.Item4)
        Console.WriteLine(value);
}

What are Tuples called?

A 2-tuple is called a pair.
A 3-tuple is called a triple.
A 4-tuple is called a quadruple.
A 5-tuple is called a quintuple.
A 6-tuple is called a sextuple.
A 7-tuple is called a septuple.

Larger tuples are called n-tuples. In my next articles of this series, I will explain some of the other keywords, Attributes, Syntax, language features, the Framework feature, and some more. If you feel I should add some other important keyword that I missed then please say so in a comment so we can all cover it. Enjoy.


Recommended Free Ebook
Similar Articles