Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot alias a class name in C#.</p> <p>There are things you can do that are not aliasing a class name in C#.</p> <p>But to answer the original question: you cannot alias a class name in C#.</p> <hr> <p><strong>Update:</strong> People are confused why <code>using</code> doesn't work. Example:</p> <p><strong>Form1.cs</strong></p> <pre><code>private void button1_Click(object sender, EventArgs e) { this.BackColor = ColorScheme.ApplyColorScheme(this.BackColor); } </code></pre> <p><strong>ColorScheme.cs</strong></p> <pre><code>class ColorScheme { public static Color ApplyColorScheme(Color c) { ... } } </code></pre> <p>And everything works. Now i want to create a <em>new</em> class, and <em>alias</em> <code>ColorScheme</code> to it (so that <strong>no code needs to be modified</strong>):</p> <p><strong>ColorScheme.cs</strong></p> <pre><code>using ColorScheme = Outlook2007ColorScheme; class Outlook2007ColorScheme { public static Color ApplyColorScheme(Color c) { ... } } </code></pre> <p>Ohh, i'm sorry. This code doesn't compile:</p> <p><img src="https://i.stack.imgur.com/GybrT.png" alt="enter image description here"></p> <p>My question was how to <em>alias</em> a class in C#. It cannot be done. There are things i can do that are <em>not</em> aliasing a class name in C#:</p> <ul> <li>change everyone who depends on <code>ColorScheme</code> to <code>using</code> <code>ColorScheme</code> instead (code change workaround because i cannot alias)</li> <li>change everyone who depends on <code>ColorScheme</code> to use a factory pattern them a polymorphic class or interface (code change workaround because i cannot alias)</li> </ul> <p>But these workarounds involve breaking existing code: not an option. </p> <p>If people depend on the presence of a <code>ColorScheme</code> class, i have to actually copy/paste a <code>ColorScheme</code> class.</p> <p>In other words: i cannot alias a class name in C#.</p> <p>This contrasts with other object oriented languages, where i could define the alias:</p> <pre><code>ColorScheme = Outlook2007ColorScheme </code></pre> <p>and i'd be done.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload