|
Assigning multiple strings to "" will also create only one object - the compiler will intern constant strings automatically.
|
|
|
|
|
string can have the value of null, Empty or "". Empty and null = no memory usage, i far as i know.
use this method to check the string: String.IsNullOrEmpty
Heres what they wrote on http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx[^]
Brad Abrams wrote: As David implies, there difference between String.Empty and “” are pretty small, but there is a difference. “” actually creates an object, it will likely be pulled out of the string intern pool, but still… while String.Empty creates no object… so if you are really looking for ultimately in memory efficiency, I suggest String.Empty. However, you should keep in mind the difference is so trival you will like never see it in your code…
As for System.String.Empty or string.Empty or String.Empty… my care level is low
And by the way, please DO use google because they have the answer http://www.google.com[^]
Please read the first posting by Chris Maunder in this Forum http://www.codeproject.com[^]
With greate code, comes greate complexity, so keep it simple stupid...
modified on Friday, June 27, 2008 6:22 AM
|
|
|
|
|
|
Paw Jershauge wrote: string can have the value of null, Empty or "".
That is not really correct.
The null value is not a string value, it's the string reference that is null.
The Empty value is not a value, it's just a predefined constant that is an empty string ("").
Paw Jershauge wrote: Empty and null = no memory usage
That is not really correct.
As Empty is just a string constant, "" and Empty work exactly the same, and use the same amount of memory. In both cases they are literal strings, so they are predefined in the assembly data, not allocated on the heap.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: so they are predefined in the assembly data, not allocated on the heap.
I agree with everything you said, except that part. Surely, they must be on the heap? I did some digging around here[^] and found that they are valid heap objects (You can dump contents, track gcroots etc.).
|
|
|
|
|
string.Empty creates a reference to an empty string that already exists. "" creates a new empty string. string.Empty is more efficient.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: "" creates a new empty string
I think strings are interned. So if there is already some reference to "", it will be reused, I am not sure though.
|
|
|
|
|
Christian Graus wrote: "" creates a new empty string.
No, literal strings are created when the code is compiled, just as the literal string that is the value of string.Empty. They both just copy a reference to an already existing string.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
As the string.Empty property returns a reference to an empty string literal that already exist in the mscorlib assembly, you don't have to create another literal string in your assembly. Other than that there is no difference in the generated code, they both use a literal string that already exists.
The most relevant reason I have found, though, is that the code gets clearer. If you always use string.Empty when you want an empty string, it's obvious what your intention was. If you then see "" in the code you know that the contents of the string was accidentally removed or left out.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: The most relevant reason I have found, though, is that the code gets clearer. If you always use string.Empty when you want an empty string, it's obvious what your intention was. If you then see "" in the code you know that the contents of the string was accidentally removed or left out.
That's the clearest explanation for it that I've seen in a while.
|
|
|
|
|
Agreed. This is definitely a coding style issue.
Debating relative allocation performance or stack overhead for the nul string is just plain retarded imho.
|
|
|
|
|
|
Hi I'm suffering with following error "Must declare the scalar variable "@name_txtbx". " here is my code
public void Bindgrid()
{
try
{
dt.Clear();
string query = "Select * From consumer where name_con like ' + txtbx_name.Text + %' AND ward_no = " + txtbx_ward.Text + "";
query = "select * from consumer where name_con = '" + txtbx_name.Text + "' And name_con_father = '" + txtbx_con_father.Text + "' And ward_no = " + txtbx_ward.Text + " ";
con.ConnectionString = constr;
cmd.Parameters.AddWithValue("@name_txtbx", txtbx_name.Text);
cmd.Parameters.AddWithValue("@ward_txtbx", txtbx_ward.Text);
cmd.Parameters.AddWithValue("@fname_txtbx", txtbx_con_father.Text);
cmd.CommandText = "select * from consumer where name_con like % @name_txtbx %OR name_con_father like % @fname_txtbx % Or ward_no like % @ward_txtbx ";
query = "select * from consumer where name_con = @name_txtbx %OR name_con_father like % @fname_txtbx % Or ward_no like % @ward_txtbx ";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
da.Fill(dt);
gv_search.DataSource = dt;
gv_search.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
please help me out, I'm new in programming field
thanks
|
|
|
|
|
Why do you define query twice ?
You should add the Parameters AFTER you define the CommandText. However, the fact that you define query THREE times I just realised, makes me think you are adding code at random. Try to think it through and understand what each step is doing.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
|
hi Paw Jershauge
If you can Explain Exactly what you want to achieve and what have you do so far, it would be easy for us to help you.
Are you looking for a way to increament a variable on a loop or what?
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
|
Ok so heres what works in the search pattern:
\b[a-zA-Z0-9]\b\s=\s\(\b[a-zA-Z0-9]\b\s\+\s1\) = finds x = (x + 1)
But i dont know how the replace patterne should be...
With greate code, comes greate complexity, so keep it simple stupid...
|
|
|
|
|
This regex will work just fine:
((\w+)\s*=\s*\(\s*\2\s*\+\s*1\s*\))
with the replacement string
$2++
You need to replace \w+ with a proper pattern for only valid variable names though.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
|
But it's wrong. Yours will also match
x = (y + 1)
because no backreference to the first variable is used for the second, so both variables might not necessarily be the same, which violates the rule i=i+1 <=> i++
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
|
hi,
I am triying to assing values dynamically toa array string here i am triying to write
code as follows
this.openFileDialogimage.ShowDialog().Equals(DialogResult.OK);
string drname = Path.GetDirectoryName(this.openFileDialogimage.FileNames[0]);
foreach (string file in this.openFileDialogimage.FileNames)
{
int i;
for (i = 0; i <= Convert.ToInt32(openFileDialogimage.FileNames.Count()); i++)
{
string[] str = new string[Convert.ToChar(openFileDialogimage.FileNames.Count())];
}
}
help me how to procede,
how i can send the file name which has be selected in open dialog box to a string .
Regard's,
Maruthi.
|
|
|
|
|
The FileNames property already returns an array of strings, so all you need is:
if (this.openFileDialogimage.ShowDialog() == DialogResult.OK) {
string[] str = this.openFileDialogimage.FileNames;
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|