Hidden Gems of C#: Exploring lesser known C# Language Features Part I
Tags: programming, csharp, dotnet, productivity
Published: 2022-12-17 19:03:30

C# is a powerful and popular programming language, with a rich set of features that allow developers to build a wide range of applications. However, like any language, C# has a number of lesser-known features that can be useful in certain situations. In this article, we'll explore some of these hidden gems and see how they can be used in practice.

## Volatile

The volatile keyword is used to indicate that a field can be modified by multiple threads simultaneously. When a field is marked as volatile, the compiler and runtime will take extra steps to ensure that its value is always up-to-date, even in the presence of concurrent access.

Here's an example of how to use volatile:

```csharp
public class Counter
{
    private volatile int _count;

    public int Count
    {
        get { return _count; }
        set { _count = value; }
    }
}

```

## extern alias

The extern alias keyword is used to specify an alias for a namespace when using a type from another assembly. This can be useful when you have multiple assemblies that contain types with the same names, and you want to disambiguate them.

Here's an example of how to use extern alias:

```csharp
extern alias MyLibrary;

using System;
using MyLibrary::MyNamespace;

public class Program
{
    static void Main()
    {
        Console.WriteLine(typeof(MyNamespace.MyType));
    }
}

```

## __arglist

The __arglist keyword is used to specify a variable number of arguments in a method declaration. It allows you to pass a variable number of arguments to a method, without using the params keyword.

Here's an example of how to use __arglist:

```csharp
public static void PrintNumbers(__arglist)
{
    var args = new ArgIterator(__arglist);
    while (args.GetRemainingCount() > 0)
    {
        Console.WriteLine(__refvalue(args.GetNextArg(), int));
    }
}

```

## __makeref and __reftype

The __makeref and __reftype keywords are used to work with managed object references in an unsafe context. The __makeref keyword creates a reference to an object, while the __reftype keyword returns the type of an object that is being referenced.

Here's an example of how to use __makeref and __reftype:

```csharp

unsafe void ModifyString(string s)
{
    TypedReference tr = __makeref(s);
    if (__reftype(tr) == typeof(string))
    {
        // Modify the string through the typed reference
    }
}

```

## as and is keywords

The as and is keywords are used to perform type conversions and type checks, respectively. The as operator performs a type conversion, and returns null if the conversion fails. The is operator returns a boolean indicating whether an object is of a specific type.

Here's an example of how to use as and is:

```csharp
object obj = "hello";
string s = obj as string;
if (s != null)
{
    Console.WriteLine(s.Length); // 5
}

if (obj is string)
{
    Console.WriteLine(((string)obj).Length); // 5
}

```

## Coalesce Nulls

The coalesce nulls operator, represented by the ??= operator, is used to assign a value to a nullable type only if it is currently null. This can be useful when you want to set a default value for a nullable type, without overwriting any existing values.

Here's an example of how to use the coalesce nulls operator:

```csharp
int? x = null;
x ??= 42; // x is now 42
x ??= 24; // x is still 42

```

## where T: new

The where T: new constraint is used to specify that a type parameter must have a public parameterless constructor. This can be useful when you want to ensure that a type can be instantiated using the default constructor.

Here's an example of how to use the where T: new constraint:

```csharp
public class MyClass<T> where T: new()
{
    public T CreateInstance()
    {
        return new T();
    }
}

```

## global::

The global:: prefix is used to specify that a type or member refers to the global namespace, rather than a namespace with the same name in the current context. This can be useful when you want to disambiguate types or members that have the same name as a namespace.

Here's an example of how to use global::

```csharp
namespace MyNamespace
{
    class MyClass
    {
        global::System.Int32 x; // refers to the Int32 type in the global namespace
    }
}

```

## Conclusion

In this article, we explored some of the lesser-known features of C#, including volatile, extern alias, __arglist, __makeref and __reftype, as and is, coalesce nulls, where T: new, and global::. While these features may not be used frequently, they can be useful in certain situations and are worth knowing about. Understanding these features can help you write more efficient and effective code in C#, and can also deepen your understanding of the language as a whole.

## Join Me

Follow me on [Twitter](https://twitter.com/volkanalkilic) and [Linkedin](https://www.linkedin.com/in/volkanalk%C4%B1l%C4%B1%C3%A7/) for more content.