|
bashiwala wrote: So, you mean, checking length ==0 is more performant than comparing with empty string, right ?
Yes. Comparing strings aren't very expensive, but it still involves things like getting the culture info to use for the comparison. Comparing the length is a simple integer comparison.
bashiwala wrote: By the way, if I use,
if(a.Trim().Length == 0)
then, am I causing same performance cost as (a.Trim() == "") ?
No, checking the length is still somewhat faster than comparing two strings.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hi,
Thanks for your suggession. I just found some exception where comparing Empty String is necessary than comparing zero length.
For example, I am checking if any Key in a NameValueCollection is Empty or not,
I can check like this,
if(MyCollection["myKey"] == String.Empty) ...
this works fine if the key does not exist, but if i use
if(MyCollection["myKey"].Length == 0) ...
then I get Exception "Object Reference Not set to an instance of an object"....
Same result I got when I check the Emptyness of a string value property like this
public string myProperty
{
get { return "hi";
set { if(value.Length == 0) ...; } // Exception is thrown, but if I use value == String.Empty, it works..
}
|
|
|
|
|
Do you really mean that if the value is null, you want to use the value?
Otherwise, there is a method in the string class for checking if a value is null or empty:
if (string.IsNullOrEmpty(MyCollection["myKey"]) ...
set { if (string.IsNullOrEmpty(value)) ...; }
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
But in that case you should check MyCollection.ContainsKey("myKey") before trying to reference the value.
Plus, won't if(MyCollection["myKey"] == String.Empty) return false ? The same as a non-empty string?
|
|
|
|
|
There isn't any performance difference. Checking if the string length is 0 is probably the better way to go, as Guffa said.
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
i think
TextBox1.Text.Trim().Equals(string.Empty)
would be a better option
|
|
|
|
|
If you are using .NET 2.0, you could use System.String.IsNullOrEmpty(a) .
-----
You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
|
how i have a gridimage for load map with episodic block in c#
i have a map blockly
i want with mouse move on map load block
please help me
|
|
|
|
|
Ok now i am creating a button to send data to a website with POST method
it works fine but the problem is when i press the button it shows me invalid referrer
Is there anyway to change webBrowser referrer?
|
|
|
|
|
Hi,
I'm trying to unbox a value stored in ViewState using the code below:
I can't seem to figure out what I'm doing wrong, can someone please perhaps point out what I'm doing wrong or perhaps an alternative?
<br />
(Int32)ViewState["totalLightsWatt"] = ((Int32)ViewState["totalLightsWatt"]) + (Int32.Parse(txtLightsQty.Text.Trim().ToString()) * Int32.Parse(cboLightsItems.SelectedValue.ToString()));<br />
Thanks.
R
|
|
|
|
|
hi,
you can't cast the left hand side, just drop the first (Int32) .
If your expression (that is the right hand side) does not have the type required by
the left hand side, make sure it is in parentheses (it is now) and cast it as in
ViewState["totalLightsWatt"] = (viewstatetype)(....); .
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi Luc
Thank you very much for your reply.
Your solution made my error very clear and I get what I did wrong now.
Thanks once again.
|
|
|
|
|
hi
how can i attach file to mail and send it using c# code ?
thank's
|
|
|
|
|
With a
System.Net.Mail.MailMessage
and a
System.Net.Mail.SmtpClient
Now I see you posted this twice and apparently ignored the response to your earlier post. You don't deserve an answer.
|
|
|
|
|
You asked this question yesterday here[^]. They replyee gave you the proper, correct answer. Why did you re-post the same question? Were you looking for someone to respond again, but do your work for you this time? This isn't rent-a-coder. People will give you information to get you going on the right path.
I'm going to become rich when I create a device that allows me to punch people in the face over the internet.
"If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
|
|
|
|
|
Hi all,
I am currently developing a windows application using C# and i am using Visual Studio.Net 2005.
I need this application to be a startup program. i need to create a system tray icon and add
it to my windows form. Then i have to edit the Double_Click event of the NotifyIcon control
such that the form will be minimized/maximized + set the ShowInTaskbar true/false depending
on whether the user wants to show or hide the application.
How should I do ?
Thanks in advance for any replies.
cheers,
peaceziz
|
|
|
|
|
|
How do you get it??
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
This[^] is what I got after a google.
|
|
|
|
|
Ok, ok, sorry guys, I understand
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
|
Thanks
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
I have a c# project and native c++ dll(with code) the dll is attached to c# code is there any way in which i can put a break point in c++ code and track down.i Use visual studio 2005
|
|
|
|
|
Yes, Visual Studio supports both C# projects and C++ projects inside a single solution. Add both projects to a solution in VS, you should be able to set a breakpoint in your C++ code.
|
|
|
|