19

I read that all primitives fall under the System namespace. If I comment out using System I would expect there to be a build error in my program, however it is running successfully. Why is this?

Attached the snap of my sample program.

John Smith
  • 7,243
  • 6
  • 49
  • 61
Rajesh Pawde
  • 399
  • 2
  • 11
  • 4
    _Even_ if you delete all namespaces, it will work :) Just a hint, if you say `Int32` instead of `int`, it won't work. – Soner Gönül Aug 08 '15 at 14:23
  • 7
    `int` is an *alias* for `System.Int32` (which you would not be able to use) – Alex K. Aug 08 '15 at 14:23
  • 1
    @AlexK.I am able to use int even though I comment using System; – Rajesh Pawde Aug 08 '15 at 14:28
  • 6
    @RajeshPawde Yes because `int` is _already_ `System.Int32`. It doesn't need _any_ namespace because it has _already_. Think about like this; you are using `Console.WriteLine` with `using System`, right? What if you have `System.Console.WriteLine`, do you need `using System` for that? No, because it has _already_. This is the same as for `System.Int32` which is aliased by `int`. – Soner Gönül Aug 08 '15 at 14:32
  • possible duplicate of [C#, int or Int32? Should I care?](http://stackoverflow.com/questions/62503/c-int-or-int32-should-i-care) – Uwe Keim Aug 08 '15 at 14:34
  • @UweKeim IMO they are related, but asking different questions fundamentally; this question also gets into the topic of using fully qualified types, without the using directive – John Smith Aug 08 '15 at 16:08
  • 1
    @iliketocode Agree with you both the questions are different. – Rajesh Pawde Aug 08 '15 at 16:40

3 Answers3

22

It's because int is an alias for System.Int32, and since the "Int32" is already prefixed with its namespace (ie. "fully qualified"), the syntax is legal without having to specify using System; at the top of your code.

The MSDN snippet below describes this concept-

Most C# applications begin with a section of using directives. This section lists the namespaces that the application will be using frequently, and saves the programmer from specifying a fully qualified name every time that a method that is contained within is used. For example, by including the line:

using System;

At the start of a program, the programmer can use the code:

Console.WriteLine("Hello, World!");

Instead of:

System.Console.WriteLine("Hello, World!");

System.Int32 (aka "int") would be the latter. Here is an example of this in code -

//using System;

namespace Ns
{
    public class Program
    {
        static void Main(string[] args)
        {
            System.Int32 i = 2;    //OK, since we explicitly specify the System namespace
            int j = 2;             //alias for System.Int32, so this is OK too
            Int32 k = 2;           //Error, because we commented out "using System"
        }
    }
}

Since line 11 is not fully qualified / aliasing a fully qualified type, using System; would need to be uncommented for the error to go away.

Additional references-

John Smith
  • 7,243
  • 6
  • 49
  • 61
  • 1
    Either my monitor's gamma is out or your vision is that of a bee ;) (color scheme) –  Aug 08 '15 at 15:17
  • 1
    Yes.. I usually code on a black background. Just switched the colors to white, this should be a little better. – John Smith Aug 08 '15 at 18:47
  • Hehe that's better. I use the dark scheme too it was just _dark blue_ does crazy stuff to my eyes ;) –  Aug 09 '15 at 02:48
8

As was mention before int is an alias of System.Int32 type. The alias of primitive types are implicitly known by the C# language. Here is the list:

object:  System.Object
string:  System.String
bool:    System.Boolean
byte:    System.Byte
sbyte:   System.SByte
short:   System.Int16
ushort:  System.UInt16
int:     System.Int32
uint:    System.UInt32
long:    System.Int64
ulong:   System.UInt64
float:   System.Single
double:  System.Double
decimal: System.Decimal
char:    System.Char

So, for these aliases, also known as simple types, you don't need to specify any namespace.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
4

When you use int, you are basically putting in System.Int32. Since this is the fully qualified type name, you don't actually need using System;

Your program would work if you did

 System.Int32 num = 0;

even without the using

potatopeelings
  • 40,709
  • 7
  • 95
  • 119