Example (note the case):
string s = \"Hello world!\";
String s = \"Hello world!\";
What are
New answer after 6 years and 5 months (procrastination).
While string
is a reserved C# keyword that always has a fixed meaning, String
is just an ordinary identifier which could refer to anything. Depending on members of the current type, the current namespace and the applied using
directives and their placement, String
could be a value or a type distinct from global::System.String
.
I shall provide two examples where using
directives will not help.
First, when String
is a value of the current type (or a local variable):
class MySequence
{
public IEnumerable String { get; set; }
void Example()
{
var test = String.Format("Hello {0}.", DateTime.Today.DayOfWeek);
}
}
The above will not compile because IEnumerable<>
does not have a non-static member called Format
, and no extension methods apply. In the above case, it may still be possible to use String
in other contexts where a type is the only possibility syntactically. For example String local = "Hi mum!";
could be OK (depending on namespace and using
directives).
Worse: Saying String.Concat(someSequence)
will likely (depending on using
s) go to the Linq extension method Enumerable.Concat
. It will not go to the static method string.Concat
.
Secondly, when String
is another type, nested inside the current type:
class MyPiano
{
protected class String
{
}
void Example()
{
var test1 = String.Format("Hello {0}.", DateTime.Today.DayOfWeek);
String test2 = "Goodbye";
}
}
Neither statement in the Example
method compiles. Here String
is always a piano string, MyPiano.String
. No member (static
or not) Format
exists on it (or is inherited from its base class). And the value "Goodbye"
cannot be converted into it.