Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# public type alias?

Tags:

c#

.net

What I want to do is write some classes in C# in a new namespace that act as wrapper classes for classes in another namespace. Sometimes a wrapper class is not needed but I still want a corresponding class in the new namespace. And I want an exact copy of the class. Is there a way to define the class in the new namespace by referring to the definition of another class? In other words I want an alias.

To clarify what I mean, if the existing namespace is named "Namespace1" and the new namespace is named "Namespace2", using code like this in Namespace2:

using Class1 = Namespace1.Class1;

would not work because Namespace2.Class1 would not exist. Class1 would only be aliased "private" to Namespace2 and not "public" to Namespace2. If I could use Namepsace2.Class1 from outside the namespace, and if that would still refer to Namespace1.Class1, then that would be what I want.

I figured there might be a way to accomplish this with attributes or reflection maybe. If there were some pre-processor directives or macros that could copy code that would work too, but obviously C# doesn't have anything like that.

like image 412
YWE Avatar asked Jan 18 '10 20:01

YWE


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You can use Using to create an alias to a type:

using Project = PC.MyCompany.Project;
like image 182
Michael La Voie Avatar answered Sep 27 '22 19:09

Michael La Voie