2

In C# if I use a type alias like string instead of (System.String) then I do not need to add a using System; directive - it compiles just fine.

However, if I change the type from the alias to the aliased Type explicitly - System.String - then it will not compile without the using directive. This seems to be true of all primitive Types (int/Int32, bool/Boolean) etc.

Why does the compiler import System for me when using an alias but not when using the actual type name?

thisextendsthat
  • 1,099
  • 7
  • 23
  • Most likely because they are aliases and reserved words and the clr understands what they are. also because someone made the decisions for that to be the case. – TheGeneral Jan 16 '19 at 09:29
  • 1
    question goes to microsoft, this is how they have developed their framework. may be you can just have look on this question https://stackoverflow.com/questions/7074/what-is-the-difference-between-string-and-string-in-c/48120399 , it has many discussion on the same (mainly the string and System.String) – Arunprasanth K V Jan 16 '19 at 09:31
  • 2
    The compiler doesn't "import" `System` when you use an alias. It's exactly like if you had written `System.String` where you wrote `string` (which despite what you say at the start of your second paragraph, does compile without `using`). It's `String` without `System.` at the start that doesn't compile. – Damien_The_Unbeliever Jan 16 '19 at 09:37
  • I had figured it dealt with native types vs boxed types. System.String is a class or struct with optimization attributes helping the compiler optimize the overhead away? Where string is part of the language definition. That is similar to how Java works. Boxing and a unboxing. But I don’t think Java optimizes/inlines them to be the same. .net core would help you see if this is the case. – TamusJRoyce Jan 16 '19 at 10:23

3 Answers3

0

You only need to import the namespace if you are not using the full, qualified name of the type:

namespace Test {
  class Cxx {
     public System.String _exampleField; 
  }
}

vs:

using System;

namespace Test {
  class Cxx {
     public String _exampleField; 
  }
}

You can import a single type from a namespace using an alias.

The built-in aliases work in a similar way to the ones you write; if instead of the built-in alias string you had your own, they you would not need to import the whole System namespace:

using mystring = System.String;

namespace Test {
  class Cxx {
     public mystring _exampleField; 
  }
}

If the only types from the System namespace you use are the ones which have aliases, you don't need the import.

namespace Test {
  class Cxx {
     public string _exampleField; 
  }
}
Pete Kirkham
  • 46,814
  • 5
  • 86
  • 159
0

The aliases are more "as if" at the top of every file, the compiler was inserting

using string = System.String;
using int = System.Int32;
using decimal = System.Decimal;

Etc.

I don't believe that's how the compiler actually implements1 the built-in aliases, but that's the overall effect. When you use using alias directives, you don't also need to have a using directive for their enclosing namespace, and it doesn't have the effect of also pulling it's enclosing namespace into scope.


1My search-foo in the Roslyn github repository is letting me down.

Damien_The_Unbeliever
  • 220,246
  • 21
  • 302
  • 402
0

There is practically no difference between both string and System.String but still String the C# keyword string maps to the .NET type System.String - it is an alias that keeps to the naming conventions of the language.

Also string is a keyword (an alias in this case) whereas String is a type.

Note : If you use Visual Studio 2015 or + and try to use String the program suggests you to "simplify your code", carrying it to string

So you can avoid the System.String and go with string no problems.

enter image description here

Arunprasanth K V
  • 17,147
  • 8
  • 33
  • 63