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# Using Alias Example

Use the using alias feature, which helps resolve ambiguities in referencing type names.
Using Alias. A using alias directive introduces new type names. These point to any existing type or namespace. This provides more flexibility should the implementation need to change. This feature has a limited range of use in the C# language.
Example. The using alias directive syntax requires the "using" keyword and then the equals sign. Then an existing type name or namespace is required. Here we map the type "Cat" to the type "System.Text.StringBuilder".

Then: In the program, the type Cat can be used as a StringBuilder. This example makes the program more confusing.

C# program that shows using alias directive using System; using Cat = System.Text.StringBuilder; class Program { static void Main() { Cat cat = new Cat(); cat.Append("sparky"); cat.Append(100); Console.WriteLine(cat); } } Output sparky100
Drawback. This can be used to create extremely confusing and non-standard programs. If you want to create extremely confusing programs, this feature can help. But don't expect to be admired for this goal.
Discussion. What are some practical uses of the using alias directive? Sometimes, an entire program would want to use one type implementation for DEBUG mode, and a different implementation for RELEASE mode.

And: You could use an alias instead of the actual type names. Then you could use #ifdef and #else to wrap the using alias directive.

DirectivesIf, Elif, Endif
Resolve ambiguities. Say you have a custom StringBuilder type in the namespace Perls.Animals.StringBuilder. If you include System.Text and this other namespace, the using alias directive can specify what StringBuilder actually points to.StringBuilder
Summary. As with the extern alias directive, the using alias directive is used to resolve ambiguities in programs. It can also enable you to swap implementations for a type based on compile-time flags.Extern

Warning: The major drawback is that it can be abused to create programs that are extremely confusing.

© 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