web analytics
Working with Strings in C# Options
codeling
Posted: Wednesday, March 22, 2017 10:03:13 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 758
Points: 3180

Literal String and Interl Pool

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

The String.Intern method uses the intern pool to search for a string equal to the value of str. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to str is added to the intern pool, then that reference is returned.

In the following example, the string s1, which has a value of "MyTest", is already interned because it is a literal in the program. The System.Text.StringBuilder class generates a new string object that has the same value as s1. A reference to that string is assigned to s2. The Intern method searches for a string that has the same value as s2. Because such a string exists, the method returns the same reference that is assigned to s1. That reference is then assigned to s3. References s1 and s2 compare unequal because they refer to different objects; references s1 and s3 compare equal because they refer to the same string.

string s1 = "MyTest";
string s2 = new StringBuilder().Append("My").Append("Test").ToString();
string s3 = String.Intern(s2);
Console.WriteLine((Object)s2 == (Object)s1); // Different references.
Console.WriteLine((Object)s3 == (Object)s1); // The same reference.

//or using Object.ReferenceEquals() method
Console.WriteLine("Object.RefEq(s1, s2): {0}",
        Object.ReferenceEquals(s1, s2)); // Different references.
Console.WriteLine("Object.RefEq(s1, s3): {0}",
        Object.ReferenceEquals(s1, s3)); // The same reference.
Sponsor
Posted: Wednesday, March 22, 2017 10:03:13 PM
codeling
Posted: Wednesday, March 22, 2017 10:03:52 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 758
Points: 3180

Instances of String are immutable in the sense that once you create them, you cannot change them.If you call the ICloneable.Clone method on a string, you get an instance that points to the same string data as the source. In fact, ICloneable.Clone simply returns a reference to this. This is entirely safe because the String public interface offers no way to modify the actual String data. In fact, if you require a string that is a deep copy of the original string, you may call the Copy method to do so.

// Sample for String.Copy()
using System;

class Sample {
    public static void Main() {
    string str1 = "abc";
    string str2 = "xyz";
    Console.WriteLine("1) str1 = '{0}'", str1);
    Console.WriteLine("2) str2 = '{0}'", str2);
    Console.WriteLine("Copy...");
    str2 = String.Copy(str1);
    Console.WriteLine("3) str1 = '{0}'", str1);
    Console.WriteLine("4) str2 = '{0}'", str2);
    }
}
/*
This example produces the following results:
1) str1 = 'abc'
2) str2 = 'xyz'
Copy...
3) str1 = 'abc'
4) str2 = 'abc'
*/
codeling
Posted: Wednesday, March 22, 2017 10:10:32 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 758
Points: 3180

String.Join method concatenates all the elements of a string array, using the specified separator between each element.

The following code convert an array of integers to a comma-separated string

int[] arr = new int[5] {1,2,3,4,5};

string.Join(",", arr); // "1,2,3,4,5"
Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2017 Digcode.com. All rights reserved.