Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:


What is the difference between

String s;

String s = new String(new char[] {'H'});
Posted
Updated 20-Jul-13 6:55am
v2
Comments
Arpit Shrivastava 20-Jul-13 6:04am    
please google it before putting here a question....
BiteForce 20-Jul-13 6:04am    
The difference is:
String s; /*in this case s isn´t already declared, so you can´t say like s.Replace(string, string); neither you can do any other kind of manipulation.

String s = new String(); /*in this case, the variable is already declared and you can work with s.
kanha.460 20-Jul-13 13:17pm    
string s = "hello";
Console.WriteLine(s);
string sa = new string(new char[] {'H'});
Console.WriteLine(sa);
string a=s.Replace('h', 'E');
Console.WriteLine(s);
Console.WriteLine(a);

We can do everything with both the codes. I have tried this code and it works prefectly.
kanha.460 20-Jul-13 13:18pm    
string s = "hello";
Console.WriteLine(s);
string sa = new string(new char[] {'H'});
Console.WriteLine(sa);
string a=s.Replace('h', 'E');
Console.WriteLine(s);
Console.WriteLine(a);

We can do everything with both the codes. I have tried this code and it works perfectly.
[no name] 20-Jul-13 15:56pm    
Yeah so? What is the real question here? Why update a question that you have an answer to and not provide any meaningful information? You could easily find the "difference" between those variables just by running your code through the debugger.

Ignoring that it won't compile, because there is no String constructor which takes no parameters...

C#
String s;
Declares a variable which can hold a reference to a string instance.
C#
String s = new String();
Declares the variable and assigns an empty string instance to the variable.

It's a bit like a water glass: the first version creates the glass, and the second fills it with a liquid. If you only declare the glass, you can't do anything with it, like drink the liquid, or stir the liquid, or freeze the liquid.
You have to assign (fill) the string (water) instance to the variable (glass) before you can do anything with it.



"My question is:
If i can manipulate both the string values then what's the difference between both types of declaration. i.e. string s; and string s=new string (new char[] {'h'});
I can use all the string function by using string s; only then what's the meaning of

C#
string s=new string(new char[] {'h'});

both are same.
I am asking the difference."



No, you can't.
If you use
C#
String s;
then using any of the string functions on s will
a) Not compile because you will get a "Use of unassigned local variable 's'" error,
and
b) Throw an exception: "Object reference not set to an instance of an object."
Both of these are down to s being capable of holding an instance of a string, but not actually doing so.

When you use any of the forms:
C#
string s = ...
such as
C#
string s = "My String";
string s = new string(new char[] {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g'});
string s = new string('X', 6);
string s = new string(new char[] {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g'}, 3, 5);

Then you are assigning a value to the variable, and giving it a reference to a string instance.

The various ways of assigning a value to the variable just give you different options for doing the same thing:
C#
string s = "h";
string s = new string(new char[] {'h'});
string s = new string('h', 1);

will all end up with the same thing: s containing a reference to a string instance that contains a single character: 'h'

The different options just let you do things the way your code needs: because a string is effectively just an array of char values.

In fact it is perfectly legal to say:
C#
for (int i = 0; i < s.Length; i++)
    {
    Console.WriteLine(s[i]);
    }
Or
C#
foreach (char c in s)
    {
    Console.WriteLine(c);
    }
The only thing you can't do is:
C#
s[4] = '$';
because strings are immutable which means that once created, you cannot change it in any way - just make a copy with a difference in it. (This can be ignored at the moment, but it becomes significant later)
 
Share this answer
 
v2
Comments
kanha.460 21-Jul-13 0:05am    
My question is:
If i can manipulate both the string values then what's the difference between both types of declaration. i.e. string s; and string s=new string (new char[] {'h'});
I can use all the string function by using string s; only then what's the meaning of
string s=new string(new char[] {'h'});
both are same.
I am asking the difference.
I wonder what is the cause of your question.
If you try it out by compiling and executing, you'll see the difference of the symptoms.

E.g.

C#
static void Main(string[] args)
{
    {
        string s;
        Console.WriteLine(s); // <--- compilation error: "Use of unassigned local variable 's'"
    }
    {
        string s = new string(new char[] {'H'});
        Console.WriteLine(s);
    }
}


The compiler tells you "what's the difference": one is not assigned any value before it is used.

[EDIT1]
string is a reference type with value semantics. If used as not explicitly initialized field in a class, the implicit initial value (of a reference type) is null. In contrast: if used as local variable in some code block, the compiler complains if not initialized nor assigned some value to the string.

E.g. this compiles without error (versus the code sample above that does not compile):
C#
class MyClass
{
    public string _s;
}

static void Main(string[] args)
{
    {
        MyClass a = new MyClass();
        Console.WriteLine(a._s == null); // Output: True
    }
}

[/EDIT1]

Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900