New to c, simple question:

  • Thread starter Thread starter Edwin Knoppert
  • Start date Start date
E

Edwin Knoppert

Should i use string or String ?
string seems 'old-fashioned' (ansi?) c string while String is depending on
the using/system stuff.
They might be equal and can be intermixed?

Maybe it's an ansi and unicode matter?

Other vartypes are there as well like byte[] and Byte[]
 
Hi,

Edwin said:
Should i use string or String ?
string seems 'old-fashioned' (ansi?) c string while String is depending on
the using/system stuff.
They might be equal and can be intermixed?

Maybe it's an ansi and unicode matter?

Other vartypes are there as well like byte[] and Byte[]

string is a value type, while String is an object. In other words, when
you use String, you get all the methods that the object defines, for
example Split, Trim, etc...

In C#, it doesn't make a big difference is you use String or string,
because the value type is automatically casted to the Object if needed.
In my experience, having discussed this with a few fellow programmers,
this is more of a "philosophy" question. I prefer to use String
consistently, because I dislike the idea of "automatic cast". But many
of my colleagues work with string only.

HTH,
Laurent
 
Ah, i see, imo it's preferrable to use the objects then.
(Unless you want some optimized for speed stuff i guess)

Thanks,

Laurent Bugnion said:
Hi,

Edwin said:
Should i use string or String ?
string seems 'old-fashioned' (ansi?) c string while String is depending
on the using/system stuff.
They might be equal and can be intermixed?

Maybe it's an ansi and unicode matter?

Other vartypes are there as well like byte[] and Byte[]

string is a value type, while String is an object. In other words, when
you use String, you get all the methods that the object defines, for
example Split, Trim, etc...

In C#, it doesn't make a big difference is you use String or string,
because the value type is automatically casted to the Object if needed. In
my experience, having discussed this with a few fellow programmers, this
is more of a "philosophy" question. I prefer to use String consistently,
because I dislike the idea of "automatic cast". But many of my colleagues
work with string only.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Hi,

Edwin said:
Ah, i see, imo it's preferrable to use the objects then.
(Unless you want some optimized for speed stuff i guess)

Thanks,

That was part of the discussion too. IMHO, since the .NET environment is
not really fast anyway (compared to native code), it's not really an issue.

HTH,
Laurent
 
Should i use string or String ?
string seems 'old-fashioned' (ansi?) c string while String is depending on
the using/system stuff.
They might be equal and can be intermixed?

Maybe it's an ansi and unicode matter?

Other vartypes are there as well like byte[] and Byte[]

in C# "string" is simply an alias for System.String. They are
completely identical and interchangeable.
Same goes for int / System.Int32, byte / System.Byte, bool /
System.Boolean, object / System.Object and others.

Hans Kesting
 
string is a value type, while String is an object.
Not true. Both are object type and both refers to System.String regardless
of what you use.


Laurent Bugnion said:
Hi,

Edwin said:
Should i use string or String ?
string seems 'old-fashioned' (ansi?) c string while String is depending
on the using/system stuff.
They might be equal and can be intermixed?

Maybe it's an ansi and unicode matter?

Other vartypes are there as well like byte[] and Byte[]

string is a value type, while String is an object. In other words, when
you use String, you get all the methods that the object defines, for
example Split, Trim, etc...

In C#, it doesn't make a big difference is you use String or string,
because the value type is automatically casted to the Object if needed. In
my experience, having discussed this with a few fellow programmers, this
is more of a "philosophy" question. I prefer to use String consistently,
because I dislike the idea of "automatic cast". But many of my colleagues
work with string only.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Hi,
Edwin said:
Should i use string or String ?
string seems 'old-fashioned' (ansi?) c string while String is depending on
the using/system stuff.
They might be equal and can be intermixed?

Maybe it's an ansi and unicode matter?

Other vartypes are there as well like byte[] and Byte[]

string is a value type, while String is an object. In other words, when you
use String, you get all the methods that the object defines, for example
Split, Trim, etc...

In C#, it doesn't make a big difference is you use String or string, because
the value type is automatically casted to the Object if needed. In my
experience, having discussed this with a few fellow programmers, this is more
of a "philosophy" question. I prefer to use String consistently, because I
dislike the idea of "automatic cast". But many of my colleagues work with
string only.

HTH,
Laurent

NO, "string" and "String" (or "System.String") are *identical*. Try
"string.Split", works just as well as "String.Split".
"string" is simply an alias for System.String. It doesn't matter (just
a case of personal preference) which form you use. There is *no*
automatic casting involved.
And "String" (or "string") is *always* a reference type, it's just that
sometimes it seems to act like a value-type.

example code:

public class MyClass
{
public static void Main()
{
string s = "First value";
Console.WriteLine(s);
ChangeIt(s);
Console.WriteLine(s);

}

private static void ChangeIt(string str)
{
str = "Other value";
}

}

Will output "First value" twice, which is similar to what you would
expect if "int" parameters. But in this case it is because with the
str = "Other value"; statement, you are not assigning a different value
to the same string-object that the parameter was a reference to, you
are changing the reference s to point to a different string (-object).
As you passed the reference itself by value, this change is not seen
outside the ChangeIt method.

Hans Kesting
 
Hi,

Hans said:
NO, "string" and "String" (or "System.String") are *identical*. Try
"string.Split", works just as well as "String.Split".
"string" is simply an alias for System.String. It doesn't matter (just a
case of personal preference) which form you use. There is *no* automatic
casting involved.
And "String" (or "string") is *always* a reference type, it's just that
sometimes it seems to act like a value-type.

<example stripped>

Mmmh I stand corrected! I learned something today. Apologies to the
original poster for misleading comments.

Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top