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??
}
}
<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .
%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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With