Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How string is available without using system Namespace

Can anyone tell,I'm not using system namespace but string is available as string hello = "Hello"; and does not throw any compile time error

but, if I write uppercase String it is not available.

sealed class SealedClass
{
    public void PrintSealed()
    {
        string hello = "Hello";
    }
}
like image 522
Bhuwan Pandey Avatar asked Jan 09 '23 06:01

Bhuwan Pandey


1 Answers

You don't need the using keyword to use a "piece" of library. The using keyword is only to make it easier to reference types inside namespaces.

When you write

using System;

.... ...

String hello = "Hello";

the compiler replaces it with

System.String hello = "Hello";

But you could have written directly

System.String hello = "Hello";

without the using System. But it is a pain :-)

Then string is an alias to System.String, so when you write string, the C# compiler replaces it with System.String (MSDN).

Note that to use a library you still have to reference it, but you don't reference it from the code, you reference it from the project, doing Add Reference. The mscorlib library (assembly) is automatically referenced (and you can't remove the reference)

This is a little different from Microsoft Visual C/C++, where often there is something like:

#pragma comment(lib, "somelibrary")

that is an instruction to the linker to include a .lib file (a library). In Visual C/C++ often you don't need to include explicitly a library, you can simply include its header that contains that command, and the library will be auto-magically linked by the linker.

like image 119
xanatos Avatar answered Jan 14 '23 06:01

xanatos