Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# where does the key words come from if "using system" is commented?

Tags:

c#

For example, we know that "int" type in C# is nothing but a structure which is actually System.Int32. If that so, then if "using system;" is commented in a program, then int type should not be able to use. But still int type can be used. My question is, from where these types are coming from?

//using System;
class Program
{
    static void Main() {
         int x = 0;  // it still work, though int is under System namespace, why??
    }
}
like image 509
sahossaini Avatar asked Sep 15 '13 17:09

sahossaini


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.


1 Answers

Type aliases like int, string, object, etc. are built into the language and do not require a using directive to be able to use, unlike DateTime for example.

However, if it helps you to think about it, you can consider int to be short for global::System.Int32.

class Program
{
    static void Main() {
         int x = 0;
    }
}

Translates to

class Program
{
    static void Main() {
         global::System.Int32 x = 0;
    }
}

In fact, because the type aliases are keywords, you can't even re-define them as you might expect:

public class int { } // Compiler error: Identifier expected; 'int' is a keyword

If for some reason you wanted to do this, you'd have to escape the identifier like this:

public class @int { }

int x = 0;             // equivalent to global::System.Int32
@int y = new @int();   // equivalent to global::MyNamespace.@int
like image 61
p.s.w.g Avatar answered Nov 16 '22 00:11

p.s.w.g