TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# extern alias Example

Use the extern keyword to specify an alias to a class library and eliminate conflicts.
Extern eliminates conflicts. Suppose we have 2 class libraries that contain a class that has the same name. For example, ClassLibrary1 and ClassLibrary2 both introduce the same class.
With extern, we can use both of those classes at once. This keyword has limited use in most environments. It is not an important one to study.ClassKeywords
An example. Create 2 Class Library projects and have them specify the same name class with no containing namespace. In Visual Studio change the Aliases fields to X and Y.

Then: You can create the main program, the console application, that specifies the extern aliases.

X and Y: The extern alias X and Y are meaningless unless they match the settings you specify in Visual Studio.

In Visual Studio: The X and Y aliases are mapped to the ClassLibrary1 and ClassLibrary2 DLL files.

Tip: You must specify the extern alias in your C# program, and also in the compilation system (Visual Studio).

C# program that uses extern alias syntax extern alias X; extern alias Y; using System; class Program { static void Main() { X.A._b = 1; Y.A._b = 2; Console.WriteLine(X.A._b); Console.WriteLine(Y.A._b); } } Contents of ClassLibrary1: C# public class A { public static int _b; } Contents of ClassLibrary2: C# public class A { public static int _b; } Output 1 2
Usage. In simple programs, the extern alias syntax is rarely useful. However, if you are developing a huge framework, it would definitely be useful.

Also: Extern is handy in large programs, or ones that use multiple versions of the same library at once.

Extern differentiates. This alias directive provides a way for us to specify the location of a class, not just its name. This means we can use a class A from many files with no conflicts.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf