A brief analysis of the special keyword where default in the c paradigm

  • 2020-05-17 06:18:39
  • OfStack

When looking at the source code, I saw where and default. Although default is very common, I saw its usage for the first time. Alas, the foundation is not solid!

Here are two special keywords:
1. Where keyword
The where clause is used to specify type constraints that can be used as variables for type parameters defined in a generic declaration.
1. Interface constraints.
For example, you can declare a generic class MyGenericClass, so the type parameter T implements IComparable < T > Interface:


publicclassMyGenericClass<T>whereT:IComparable{}

2. Base class constraint: indicates that a type must have the specified class as the base class (or the class itself) in order to be used as a type parameter for the generic type.
Such a constraint 1, when used, must appear before all other constraints on a parameter of that type.

class MyClassy<T,U>
whereT:class03.whereU:struct04.{
}

3. The where clause can also include constructor constraints.
You can use the new operator to create instances of type parameters. However, the type parameter must be constrained by the constructor constraint new(). The new() constraint lets the compiler know that any type parameter provided must have an accessible no-argument (or default) constructor. Such as:

publicclassMyGenericClass<T>whereT:IComparable,new()
{
//Thefollowinglineisnotpossiblewithoutnew()constraint:04.Titem=newT();
}

The new() constraint appears at the end of the where clause.

4. For multiple type parameters, one where clause is used for each type parameter
Such as:


interface MyI{}
class Dictionary<TKey,TVal>
whereTKey:IComparable,IEnumerable
whereTVal:MyI
{
publicvoidAdd(TKeykey,TValval)
{
}
}

5. You can also attach constraints to the type parameters of a generic method, such as:

public bool MyMethod<T>(Tt)whereT:IMyInterface{}

Note that the syntax for describing type parameter constraints is one for both the delegate and the method:

delegateTMyDelegate<T>()whereT:new()

In summary, Where specifies type constraints for the paradigm.

2. Default keyword
One of the problems that arises in generic classes and generic methods is how to assign default values to the parameterized type T when the following is not known in advance:

Is T a reference type or a value type?
If T is a value type, is it a numeric value or a structure?
Given a variable t of the parameterized type T, the statement t = null is valid only if T is the reference type. The statement t = 0 will only work if T is a numeric type, not a structure.

The solution is to use the default keyword, which returns null for reference types and zero for numeric types. For structures, this keyword returns each structure member initialized to zero or null, depending on whether the structures are value or reference types. The following is from GenericList < T > Class shows how to use the default keyword.


public class GenericList<T>
{
    private class Node
    {
        //...06.07.        public Node Next;
        public T Data;
    }
    private Node head;
    //...14.15.    public T GetNext()
    {
        T temp = default(T);
        Node current = head;
        if (current != null)
        {
            temp = current.Data;
            current = current.Next;
        }
        return temp;
    }
}

2. Another use of default
In an switch statement, if no case expression matches the switch value, control is passed to the statement following the optional default label. If there is no default tag, control is passed outside switch.

int id = int32.Parse(Console.ReadLine());     
switch (id)
      {
          case 1: 
              Console.WriteLine("Lee");
              break;
          case 2:
              Console.WriteLine("Tang");
              break;
          default:
              Console.WriteLine("Sorry, no one match this ID!");
              break;
      }

The where clause can also include constructor constraints. You can create instances of type parameters using the new operator. But the type parameter must be bound by the constructor constraint new() for this.

The new() constraint lets the compiler know that any type parameter provided must have an accessible no-argument (or default) constructor.

The new() constraint appears at the end of the where clause.

For multiple type parameters, one where clause is used for each type parameter

You can also attach constraints to the type parameters of a generic method

Note that the syntax for describing type parameter constraints is one for both the delegate and the method


Related articles: