Home » New in .NET 8.0 [5]: Typaliasse in C# 12.0

New in .NET 8.0 [5]: Typaliasse in C# 12.0

by admin
New in .NET 8.0 [5]: Typaliasse in C# 12.0

Since C# language version 12.0, type aliases provide the ability to define an alternative name for a type. Types can be C# types, NET base classes/structures or your own classes/structures.

Advertisement

Dr. Holger Schwichtenberg is Chief Technology Expert at MAXIMAGO, which offers innovation and experience-driven software development, including in highly critical safety-related areas. He is also head of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies with advice and training in the development and operation of software with 43 renowned experts.

The using keyword, which has already been used several times in C#, takes on a further meaning.

When you write

Numbers numbers = new int[10];

From now on you can use numbers instead of int[] Use in type declarations:

Numbers numbers = new int[10];

However, you are allowed to use the alias not use when instantiating:

Numbers numbers = new Numbers;

Unfortunately, you cannot define an alias using an alias. The following is therefore also not permitted:

using DbIntList = List;

Second example: DbInt as an alias for an int? Nullable:

using DbInt = int?;

// ermöglicht:

public DbInt LoadNumberFromDatabase()
{
try
{

}
catch (Exception)
{
return null;
}
}

DbInt n;
n = LoadNumberFromDatabase();
Console.WriteLine(n == null ? “null” : n);

Third example: Type alias for a tuple

using Measurement = (string Units, int Distance);

// ermöglicht:

public Measurement Add(Measurement m1, Measurement m2)
{
if (m1.Units == m2.Units)
{
return (m1.Units, m1.Distance + m2.Distance);
}
else
{
throw new Exception(“Units do not match!”);
}
}

Measurement m1 = (“m”, 100);
Console.WriteLine(m1.Distance + ” ” + m1.Units);

Measurement m2 = (“m”, 42);
Console.WriteLine(m2.Distance + ” ” + m2.Units);

Measurement m3 = Add(m1, m2);
Console.WriteLine(m3);

Fourth example: Type alias for a .NET class

See also  French Government Orders Apple to Remove iPhone 12 from Market over High Levels of Electromagnetic Radiation

using MyPerson = BO.Person;

Unlike the int array alias numbers, use during instantiation is permitted here:

MyPerson p = new MyPerson();
MyPerson[] pArray = new MyPerson[10];

A type alias must appear at the beginning of a file outside of all type implementations (classes, structs). It may appear before or after the using statements for namespace imports and before or after the namespace declaration. Exception: If the type alias is to apply not just to one file, but to all files in the project, then the alias must be in front of the namespace and also have the keyword global. A type alias cannot be exported for other projects. It must be declared once in every .NET project when used.

global using Measurement = (string Units, int Distance);
using BO;

namespace BL;

// Typaliasse dürfen im Namensraum stehen
using Numbers = int[];
using DbInt = int?;
using MyPerson = Person;

class MeineKlasse
{

}

(rme)

To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy