In the past I always used "" everywhere for empty string in my code
without a problem.
Now, do you think I should use String.Empty instead of "" (at all
times) ?
Let me know your thoughts. 26 2793
"an******@hotma il.com" wrote:
In the past I always used "" everywhere for empty string in my code
without a problem.
Now, do you think I should use String.Empty instead of "" (at all
times) ?
If you're checking for an empty string it's better to use:
String.IsNullOr Empty(strString Name)
As of .NET 2.0
Hi,
There is very little difference between the two.
"" is easy to write, easy to read, and near impossible to misunderstand.
It will however create an object where String.Empty would not, but the ""
object is reused throughout the lifespawn of your application so the
difference can therefore be ignored.
Neither will detect a null string so if you can have null strings you need
to either check for that as well or use String.IsNullEr Emtpy as Leon
pointed out.
if(str == null || str == "") // or
if(String.IsNul lOrEmpty(str))
If, on the other hand, you know you will never get null strings, testing
for String.Length == 0 is the fastest way.
On Thu, 23 Nov 2006 10:57:06 +0100, <an******@hotma il.comwrote:
In the past I always used "" everywhere for empty string in my code
without a problem.
Now, do you think I should use String.Empty instead of "" (at all
times) ?
Let me know your thoughts.
--
Happy Coding!
Morten Wennevik [C# MVP]
<an******@hotma il.comwrote:
In the past I always used "" everywhere for empty string in my code
without a problem.
Now, do you think I should use String.Empty instead of "" (at all
times) ?
Which do you find easier to read? That's pretty much the only
difference of any significance, IMO.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Morten Wennevik wrote:
"" is easy to write, easy to read, and near impossible to misunderstand.
It will however create an object where String.Empty would not, but the ""
object is reused throughout the lifespawn of your application so the
difference can therefore be ignored.
I was surprised to read that "" will create an object where
String.Empty will not - surely String.Empty is just a static field
that points to an interned "" constant?
Sure enough, Reflector shows that String.Empty is implemented as
class string
{
static readonly string Empty = "";
}
.... yet the below code shows that while both "" and String.Empty are
interned, "" is not reference-equal to String.Empty. What gives? I
thought the intern table was maintained across assembly boundaries, so
it shouldn't matter that String.Empty comes from mscorlib.dll while ""
comes from the app assembly.
//
using System;
namespace StringEmptyTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLi ne(Object.Refer enceEquals(Stri ng.Empty, ""));
Console.WriteLi ne(String.IsInt erned("") != null);
Console.WriteLi ne(String.IsInt erned(String.Em pty) != null);
Console.ReadLin e();
}
}
}
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
What you need to know.
And what happens if string interning is disabled? Doesn't this mean that
each time you have a "" in your code that you will have the overhead of
allocating memory on the heap and creating a new string object?
Gabriel
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
<an******@hotma il.comwrote:
>In the past I always used "" everywhere for empty string in my code without a problem.
Now, do you think I should use String.Empty instead of "" (at all times) ?
Which do you find easier to read? That's pretty much the only
difference of any significance, IMO.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Gabriel Lozano-Morán <ab***@frontbri dge.comwrote:
And what happens if string interning is disabled? Doesn't this mean that
each time you have a "" in your code that you will have the overhead of
allocating memory on the heap and creating a new string object?
String interning isn't something you can enable or disable, as far as
I'm aware - and at that point, the C# language specification is being
violated. I don't alter my programming style in order to suit
situations like that - it's like changing your style to suit a
situation where garbage collection doesn't occur.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
See: http://msdn2.microsoft.com/en-us/lib...attribute.aspx http://msdn2.microsoft.com/en-us/lib...laxations.aspx
Gabriel Lozano-Morán
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Gabriel Lozano-Morán <ab***@frontbri dge.comwrote:
And what happens if string interning is disabled? Doesn't this mean that
each time you have a "" in your code that you will have the overhead of
allocating memory on the heap and creating a new string object?
String interning isn't something you can enable or disable, as far as
I'm aware - and at that point, the C# language specification is being
violated. I don't alter my programming style in order to suit
situations like that - it's like changing your style to suit a
situation where garbage collection doesn't occur.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote
And what happens if string interning is disabled?
String interning isn't something you can enable or disable, as far as
I'm aware - and at that point, the C# language specification is being
violated.
You can't disable it. At least not that I was able to find, and I looked for
quite a while.
I wrote up a blog entry on my findings with string interning and when /
where it can save memory (at least in the case i'm worried about): http://www.coversant.com/dotnetnuke/...=88&EntryID=24
--
Chris Mullins, MCSD.NET, MCPD:Enterprise http://www.coversant.net/blogs/cmullins
Gabriel Lozano-Morán <ab***@frontbri dge.comwrote:
See: http://msdn2.microsoft.com/en-us/lib....compilerservi
ces.compilation relaxationsattr ibute.aspx
How bizarre. Using that would certainly mean you were no longer using a
language which conformed to the C# language spec. However, you would
have to *explicitly* use that attribute, so again I can't see that it's
actually a consideration to think about in normal coding.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jaime Wyant |
last post by:
Will someone explain this to me?
>>> "test".find("")
0
Why is the empty string found at position 0?
Thanks!
jw
|
by: Klaus Petersen |
last post by:
Hi ng.
I have a varchar field in my table, called Name.
I wanna do a selection, which is ordered by whether this field is empty or
not.
E.g. something like:
SELECT
|
by: Marcin Dobrucki |
last post by:
I've been having some problems with a parse error that I can't figure
out (PHP 4.3.11 on Solaris9). Sample code:
<?php
// getting strange parse errors on this
class A {
var $value;
function A() {
$this->value = 1;
}
|
by: Peter Merwood |
last post by:
I'm using some sample code from MSDN. The code includes the following
statement:
Dim content as = .Empty
I'm not familiar with the use of the square brackets. Can someone please
explain to me what the difference between "Dim content as " and
"Dim content as String". Thanks.
Peter,
|
by: Fei Liu |
last post by:
In Accellerated C++, the author recommends that in a header file one should
not declare
using std::string, using std::vector etc instead one should directly specify
the namespace specifier in code.
for example, this is bad practice:
header.h
#include <string>
using std::string;
| |
by: mcbobin |
last post by:
Hi,
Here's hoping someone can help...
I'm using a stored procedure to return a single row of data ie a
DataRow
e.g.
public static DataRow GetManualDailySplits(string prmLocationID, string
|
by: =?Utf-8?B?RWxlbmE=?= |
last post by:
I am surprised to discover that c# automatically converts an integer to a
string when concatenating with the "+" operator. I thought c# was supposed
to be very strict about types. Doesn't it seem like c# is breaking its own
rules?
This works:
int b = 32;
string a = "ABC " + b;
Result: a = "ABC 32"
|
by: =?Utf-8?B?SmVmZg==?= |
last post by:
I thought this would already be covered here, but my search turned up nothing.
In VS2005, if I use "String" to define a new variable/class, it colors it in
the Aqua color as it does other classes. But if I use "string", it colors it
in Navy blue as in C# reserved words (e.g. private, void, foreach, etc).
What is the diff?
|
by: Dave griffiths |
last post by:
Hi all
Using VB2005 on Vista with a Norwegian locale setup.
The test program has 3 textboxes the sum held in txt3.
Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this
Public Class Main
|
by: Sami |
last post by:
string = "" or string = string.Empty?
should is the proper way?
Sami
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |