You probably knew that you can use the String.Trim method to remove whitespace from the start and end of a C# string. Unfortunately, the Trim method does not remove whitespace from the middle of a string.
Of course you know that XML denotes element names with greater-than and less-than symbols, such as:
<name>value</name>
Therefore, to avoid confusing the XML parser, the greater/less symbols (and the ampersand, an HTML special character) must be encoded.
C# object equality is one of those topics that seems easy on the surface but can get a little complicated when you dig into it.
It’s easy to reverse the contents of an array using C# generics:
The params keyword specifies a variable number of arguments to be passed to a constructor or method, such as:
public Test( string arg0, float arg1, params int[] args ) { }
As discussed in a previous article, Generics provides the ability to create strongly-typed collections in C#. Unfortunately, C# currently does not support generics variance, which would allow inheritance of generic types.
When you want to share initialization code among multiple constructors, there are generally two approaches.
For performance efficiency, you may wish to cache an object within another object. That sounds easy enough to do. But what if the object cannot be found? You have to take special care to ensure the object is not fetched repeatedly.
One way to format C# strings is to add strings together using the plus-sign operator, such as:
string fileStats = "Bytes=" + bytes.ToString() + ", Pages=" + pages.ToString() + ", Words=" + words.ToString();
This code will correctly produce the desired output but is inefficient and does not scale as well.
There are multiple static “Is” methods on the Char structure that help determine a character’s category. All of these methods (except IsSurrogatePair) take a single character argument (or string and index) and return a boolean whether the character is in the corresponding category.