4

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";
    }
}
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Bhuwan Pandey
  • 514
  • 1
  • 6
  • 19
  • 1
    Get an answer in this question: [What's the difference between String and string?](http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string?rq=1) – Blas Soriano May 30 '15 at 07:45
  • That is clear, but I'm asking how I'm able to use string but not String without using System namespace. – Bhuwan Pandey May 30 '15 at 07:48
  • @BhuwanPandey It is cleary written: *string is an alias in C# for System.String. So technically, there is no difference. It's like int vs. System.Int32.* *string* isn't an alias inside the namespace `System`. It is an alias stop. – xanatos May 30 '15 at 07:50
  • Check [Built-In Types Table (C# Reference)](https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx) to learn about these alias. – Blas Soriano May 30 '15 at 07:52
  • @xantos Thanks a lot....Now I got it – Bhuwan Pandey May 30 '15 at 07:52
  • @BhuwanPandey check my answer – user786 May 30 '15 at 07:53

1 Answers1

9

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • u said:Then string is an alias to System.String, so when you write string, the C# compiler replaces it with System.String. can you please include the source where it is mentioned. thanks – user786 May 30 '15 at 07:56
  • 1
    @Alex Here there is the table: https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx – xanatos May 30 '15 at 07:59
  • I've added an MSDN link which states that string is indeed an alias. Also, +1 from me! – Stephen Kennedy Jun 07 '15 at 11:35