intereSTRINGS

‘inetreStrings’ – liked the nomenclature and the sense of humor of the author of it very much.

String vs string
Lower case string is simply an alias for System.String in the .NET Framework.

== or .Equals
A rule of thumb is that for almost all reference types e.g. objects, use Equals when you want to test equality. == compares whether two references (i.e. object instances) refer to the same object.
Primitive types can be compared for equality using ==, but in the case of strings, the variables on each side of the operators must be of type string.

Have a look at the following code and output, taken from Jon Skeet’s blog.

01    using  System;
02    
03    public class Test
04    {
05    static void Main()
06    {
07    // Create two equal but distinct strings
08    string a = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
09    string b = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
10    
11    Console.WriteLine (a==b);
12    Console.WriteLine (a.Equals(b));
13    
14    // Now let's see what happens with the same tests but
15    // with variables of type object
16    object c = a;
17    object d = b;
18    
19    Console.WriteLine (c==d);
20    Console.WriteLine (c.Equals(d));
21    }
22    }
23    The results are:
24    
25    True
26    True
27    False
28    True

@ before a string
Using @ before a string means the escape sequences are not processed. This making it easier to write fully qualified path names for example.
@”c:\Docs\Source\a.txt” // rather than “c:\\Docs\\Source\\a.txt”

Stringbuilder
String objects are immutable, and so editing them, using Replace for example, results in a entirely new string being created, with new memory allocation. The original string is left intact in memory.
The StringBuilder class is designed for situations when one needs to work with a single string and make an arbitrary number of iterative changes to it. New memory allocation is not needed.

Came across the above on Bishop’s Blog while searching about something related to VSTO.

Advertisements
This entry was posted in Code Snippets and tagged , , , , by Pranav. Bookmark the permalink.

About Pranav

A Software Engineer by profession and a Geek at heart. I love exploring new technologies and working on algorithmic problems. As of now, I write code for bread, butter and satisfaction. I like to develop applications, and generally that involves much more than writing code. It’s not just about hurling code, but producing something meaningful that has a measurable and positive impact, helps others and solves interesting problems.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s