PadLeft and PadRight pad a string to a given length with a specified character (or a space if unspecified). If the input string is longer than the padding length, the original string is returned

unchanged.

Console.WriteLine ("12345".PadLeft (9, '*')); // ****12345

Console.WriteLine ("12345".PadLeft (9)); // 12345

TrimStart and TrimEnd remove specified characters from the beginning or end of a string; Trim does both. By default, these functions remove whitespace characters (including spaces, tabs, new lines, and Unicode variations of these):

Console.WriteLine (" abc \t\r\n ".Trim().Length); // 3

Replace replaces all (non-overlapping) occurrences of a particular character or substring:

Comparing Strings

There are two basic algorithms for string comparison: ordinal and culture-sensitive. Ordinal comparisons interpret characters simply as numbers (according to their numeric Unicode value); culture-sensitive comparisons interpret characters with reference to a particular alphabet. There are two special cultures: the “current culture,” which is based on settings picked up from the computer’s control panel, and the “invariant culture,” which is the same on every computer (and closely matches

American culture).

Despite ordinal’s limitations, string’s == operator always performs ordinal case sensitive comparison.The same goes for the instance version of string.Equals when called without arguments; this defines the “default” equality comparison behavior for the string type.

The following methods allow culture-aware or case-insensitive comparisons:

The static version is advantageous in that it still works if one or both of the strings are null. StringComparison is an enum defined as follows:

public enum StringComparison

{

CurrentCulture, // Case-sensitive

CurrentCultureIgnoreCase,

InvariantCulture, // Case-sensitive

InvariantCultureIgnoreCase,

Ordinal, // Case-sensitive

OrdinalIgnoreCase

}

String order comparison

String’s CompareTo instance method performs culture-sensitive, case-sensitive order comparison. Unlike the == operator, CompareTo does not use ordinal comparison: for ordering, a culture-sensitive algorithm is much more useful. Here’s the method’s definition:

public int CompareTo (string strB);

For other kinds of comparison, you can call the static Compare and CompareOrdinal

StringBuilder

The StringBuilder class (System.Text namespace) represents a mutable (editable) string. With a StringBuilder, you can Append, Insert, Remove, and Replace substrings without replacing the whole StringBuilder.

StringBuilder’s constructor optionally accepts an initial string value, as well as a starting size for its internal capacity (default is 16 characters). If you go above this, StringBuilder automatically resizes its internal structures to accommodate (at a slight performance cost) up to its maximum capacity (default is int.MaxValue). A popular use of StringBuilder is to build up a long string by repeatedly calling Append.

StringBuilder sb = new StringBuilder();

for (int i = 0; i < 50; i++) sb.Append (i + ",");

Console.WriteLine (sb.ToString())

In our example above, the expression i + "," means that we’re still repeatedly concatenating strings. However, this incurs only a small performance cost in that the strings in question are small

and don’t grow with each loop iteration. For maximum performance, however, we could change the loop body to this: { sb.Append (i.ToString()); sb.Append (","); }

Methods: AppendLine, AppendFormat, Remove, Insert, Replace

Encoding object

Encoding utf8 = Encoding.GetEncoding ("utf-8");

Encoding chinese = Encoding.GetEncoding ("GB18030");

The most common encodings can also be obtained through dedicated static prop-
erties on Encoding:

The LocalDateTime property returns a DateTime in the current local time zone (converting it if necessary).

The DateTime property returns a DateTime in whatever zone it was specified, with a Kind of Unspecified (i.e., it returns the UTC time plus the offset).

TimeZone and TimeZoneInfo

TimeZoneInfo was introduced in Framework 3.5.

The biggest difference between the two types is that TimeZone lets you access only the current local time zone, whereas TimeZoneInfo provides access to all the world’s time zones. Further, TimeZoneInfo exposes a richer (although at times, more awkward) rules-based model for describing daylight saving time.

Tuples

Framework 4.0 introduced a new set of generic classes for holding a set of differently
typed elements. These are called tuples.

You can instantiate a tuple either via its constructor or via the static helper method Tuple.Create:

Tuples are convenient in returning more than one value from a method—or creating
collections of value pairs (we’ll cover collections in the following chapter).

An alternative to tuples is to use an object array. However, you then lose static type
safety, incur the cost of boxing/unboxing for value types, and require clumsy casts
that cannot be validated by the compiler:

Being a struct, a Guid honors value-type semantics; hence, the equality operator works.
The static Guid.Empty property returns an empty Guid (all zeros). This is often used in place of null .

Equality Comparison

There are three standard protocols that types can implement for equality comparison:

The == and != operators

The virtual Equals method in object

The IEquatable<T> interface

1.
Because object is a class (and so a reference type), object’s == operator uses referential equality to compare x and y. The result is false, because x and y each refer to different boxed objects on the heap. "==" and "!=" are operators, i.e. static and resolved at compiling time.

int x = 5, y = 5;

Console.WriteLine (x == y); // True

object x = 5, y = 5;
Console.WriteLine (x == y); // False

2.
Object.Equals method is resolved at runtime - according to the object's actual type. In this example below, it calls Int32’s Equals method, which applies value equality to the operands, returning true. With reference types, Equals performs referential equality comparison by default; with structs, Equals performs structural comparison by calling Equals on each of its fields.

3.
Since the Equals method has a parameter of type Object, a cast will be needed in order to be able to access class specific members. Tus A consequence of calling object.Equals is that it forces boxing on value types. This is undesirable in highly performance-sensitive scenarios because boxing is relatively expensive compared to the actual comparison.

The idea is that IEquatable<T>, when implemented, gives the same result as calling object’s virtual Equals method—but more quickly. Most basic .NET types implement IEquatable<T>. You can use IEquatable<T> as a constraint in a generic type:

If we remove the generic constraint, the class would still compile, but a.Equals(b) would instead bind to the slower object.Equals (slower assuming T was a value type).

Note. the runtime will use the IEquatable.Equals method only if you pass an object with the same type as defined in the interface implementation; otherwise, it will use the Object.Equals method.
Therefore, in order to align the same result of both interface and object equals() method, we need to do both work: http://www.codeproject.com/Articles/20592/Implementing-IEquatable-Properly/