2

I faced a strange confusion today. Although I read the difference between String and string in C# in this post : What is the difference between String and string in C#?. But when I try to use String with capital letter without using the namespace System, it was not recognized. Like

This code works,

using System;

String s = "";

But without using System, it gives error.

Whereas string with small letter works with and without using the System namespace.

If String and string are same things then why one works only with its namespace and other works with and without namespace as well.

Community
  • 1
  • 1
Usman Khalid
  • 3,032
  • 9
  • 41
  • 66
  • 1
    have you ensured that **Using System;** namespace is included in your class file? as you can see in the provided link *string is an alias for System.String*, so its behave as expected. – Kundan Singh Chouhan Oct 20 '12 at 10:04
  • when Using System is included then String and string both works. but When it is not included then only string with small letter works. If both are the same then why one works and other does not? – Usman Khalid Oct 20 '12 at 10:09
  • 2
    Usman, if you read that [article](http://stackoverflow.com/questions/7074/what-is-the-difference-between-string-and-string) the first line in accepted answer is "string is an alias for System.String". Its mean if you write small letter **string** it will be same as **System.String** – Kundan Singh Chouhan Oct 20 '12 at 10:11

5 Answers5

4

string is a C# construct that translates, during compilation to System.String.

Every C# program default has:

using string = System.String;
using char = System.Char;
using int = System.Int32;
using double = System.Double;

System.String is the class. You can't use the type String without having access to the System namespace (so, either fully qualified as System.String, or with a using System; directive).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

String is an alias for System.String. See the answer with 728 score on the SO answer you linked to for a list of some of the aliases. If you use the alias, it automatically grabs the System. bit, so you don't need to explicitly reference it, coz it already is.

mcalex
  • 6,628
  • 5
  • 50
  • 80
1

System.String is a class of System namespace. string reference directly to it.

When you type string, you access to all methods (and constructors, etc) of System.String.

The same thing with Nullable<T> : You can write, example

int? myInt = 2;

It's the same thing of

Nullable<int> myInt = 2;

Only Alias utility

T-moty
  • 2,679
  • 1
  • 26
  • 31
1

Well think of this as below

Lets say we have a Namespace Foo that has a Class called Boo. Now If we need to call Boo we need to include that in the project using "Using" keyword.

In case we create an alias(Not sure whether it is possible or not) for ex : Foo.Boo > NewBoo

in that case the compiler will see that as Foo.Boo

Here in String case.

When we write string that is compiled as System.String hence we do not need to do any sort of inclusion of the System Namespace to access string.

But in case of Just String we need to include it.

Pradip
  • 1,507
  • 11
  • 28
1

It is actually true, this won't compile without using System; There was a squiggle in the line String name = "me";

class Program
{

    static void Main(string[] args)
    {
        string name1 = "me";

        String name2 = "me";
    }

}

When I put mouse on string it says class System.String and when I put it on String it says the type or namespace String could not be found

codingbiz
  • 26,179
  • 8
  • 59
  • 96