Click here to Skip to main content
15,890,282 members
Home / Discussions / C#
   

C#

 
GeneralRe: Should I use new for a string? Pin
Hamed Musavi11-Jun-08 6:12
Hamed Musavi11-Jun-08 6:12 
GeneralRe: Should I use new for a string? Pin
leppie11-Jun-08 6:18
leppie11-Jun-08 6:18 
GeneralRe: Should I use new for a string? Pin
Hamed Musavi11-Jun-08 6:21
Hamed Musavi11-Jun-08 6:21 
GeneralRe: Should I use new for a string? Pin
User 665811-Jun-08 6:22
User 665811-Jun-08 6:22 
GeneralRe: Should I use new for a string? Pin
Hamed Musavi11-Jun-08 6:31
Hamed Musavi11-Jun-08 6:31 
GeneralRe: Should I use new for a string? Pin
User 665811-Jun-08 6:57
User 665811-Jun-08 6:57 
GeneralRe: Should I use new for a string? Pin
S. Senthil Kumar11-Jun-08 8:19
S. Senthil Kumar11-Jun-08 8:19 
AnswerRe: Should I use new for a string? Pin
Guffa11-Jun-08 7:23
Guffa11-Jun-08 7:23 
The new keyword has to be used when creating a new instance of a string. However, most of the time you don't create any strings directly in your code.

The most common situation is the assignment of a literal string:

string s = "asdf";

In this case, the literal string "asdf" already exists as a constant in the assembly, so you are actually only assigning the reference of an already existing string object.

In most other situations you are calling a method that creates an instance of a string, so the actual creation is not in your code. Example:

string t = s.Substring(0, 1);

The Substring method creates a new instance of the String class and returns the reference, so you are only assigning the return value in your code.

You only use the new keyword when you create a string directly in your code:

string u = new string('*', 42); // creates a string containing 42 asterisks.

Despite everything, the person most likely to be fooling you next is yourself.

GeneralRe: Should I use new for a string? Pin
Hamed Musavi11-Jun-08 7:29
Hamed Musavi11-Jun-08 7:29 
GeneralRe: Should I use new for a string? Pin
Guffa11-Jun-08 7:40
Guffa11-Jun-08 7:40 
GeneralRe: Should I use new for a string? Pin
BadKarma11-Jun-08 10:26
BadKarma11-Jun-08 10:26 
GeneralRe: Should I use new for a string? Pin
Guffa11-Jun-08 13:13
Guffa11-Jun-08 13:13 
AnswerRe: Should I use new for a string? Pin
Christian Graus11-Jun-08 6:24
protectorChristian Graus11-Jun-08 6:24 
GeneralRe: Should I use new for a string? Pin
Hamed Musavi11-Jun-08 6:29
Hamed Musavi11-Jun-08 6:29 
GeneralRe: Should I use new for a string? [modified] Pin
S. Senthil Kumar11-Jun-08 6:46
S. Senthil Kumar11-Jun-08 6:46 
GeneralRe: Should I use new for a string? Pin
Hamed Musavi11-Jun-08 7:14
Hamed Musavi11-Jun-08 7:14 
AnswerRe: Should I use new for a string? Pin
Guffa11-Jun-08 7:27
Guffa11-Jun-08 7:27 
GeneralRe: Should I use new for a string? Pin
Hamed Musavi11-Jun-08 7:30
Hamed Musavi11-Jun-08 7:30 
GeneralRe: Should I use new for a string? Pin
Christian Graus11-Jun-08 6:49
protectorChristian Graus11-Jun-08 6:49 
GeneralRe: Should I use new for a string? Pin
PIEBALDconsult11-Jun-08 15:35
mvePIEBALDconsult11-Jun-08 15:35 
QuestionInstaller Issues Pin
Russell Jones11-Jun-08 5:43
Russell Jones11-Jun-08 5:43 
AnswerRe: Installer Issues Pin
Christian Graus11-Jun-08 5:54
protectorChristian Graus11-Jun-08 5:54 
GeneralRe: Installer Issues Pin
Russell Jones11-Jun-08 6:02
Russell Jones11-Jun-08 6:02 
AnswerRe: Installer Issues Pin
leppie11-Jun-08 5:57
leppie11-Jun-08 5:57 
GeneralRe: Installer Issues Pin
Russell Jones11-Jun-08 6:01
Russell Jones11-Jun-08 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.