Some Useful Features of C#

Introduction

In this article, I am going to discuss some important facts which might be unknown to most developers. I am going to discuss about three different topics which are related to C# and are more helpful when building your application.

Why use StringBuilder over String to get better performance

Structure initialization in C#

Checked operator

Go To with Switch... Case

I am going to discuss the above list one by one.

Why use StringBuilder over String to get better performance

There are a number of articles and posts that say that StringBuilder is more efficient because it contains a mutable string buffer. .NET Strings are immutable, which is the reason why a new String object is created every time we alter it (insert, append, remove, etc.).

In the following section, I am going to explain this in more detail to give beginners a clear view about this fact.

I wrote the following code, and as you can see, I have defined a String variable and a StringBuilder variable. I then append a string to both of these variables:

To get in to the details of what happened when I appended the string, I used the RedGate .NET Reflector. The image below shows the IL of the code that I executed above. As you can see:

The String calls the Concat(str0,str1) function to append the string.

The StringBuilder calls the Append(str) function to append the string.

When I click the Concat function in Reflector, it redirects me to the code below:

As you can see, the Concat function takes two arguments and returns a String. The following steps are performed when we execute the append with the string type:

Checks if the string is null or not

Creates a string dest and allocates memory for the string

Fills the dest string with str0 and str1

Returns the dest string, which is a new string variable

So this proves that whenever I do an operation like concatenation of strings, it creates a new string because of the immutable behaviour of strings. Note: The abbove scenario occurs for all functions related to string operations.

When I click on the Append function in Reflector, it redirects me to the code below:

The Append function takes an argument of type String and returns a StringBuilder object. The following steps are performed when we execute Append with the StringBuilder type:

Get the string value from the StringBuilder object

Check if it is required to allocate memory for the new string we are going to append

Allocate memory if required and append the string

If not required to allocate memory, then append the string directly to the existing allocated memory

Return the StringBuilder object which called the function, by using the this keyword

So it returns the same object without creating a new one.

Summary

I hope this section has helped you to understand the inner details of why we should use a StringBuilder over a String to get better performance when we are doing major string manipulation in your code.

Structure Initialization in C#

Structures in C# allow us to group variables and methods. Its somewhat similar to classes but there are a number of differences between them. In this article, I am not going to discuss about that. I am going to explain how to initialize a structure.

Facts

A structure is a value type

It doesn't allow to create a parameter less constructor because it initializes the variable with default values

After doing this, when I try to compile the code, I receive the following error:

The C# compiler issues the following error:
error CS0165: Use of unassigned local variable 'PropertiesStruct'

The C# compiler informs us that it allows to use the first structure without an error, but does not allow to use the second structure which exposes the property. To resolve the issue, I wrote the below line of code to initialize the second structure:

StructProperties PropertiesStruct = new StructProperties();

And after doing this, when I compile the code, it gets complied successfully.

To find out the reason why it worked without an error when I wrote the second line, I reviewed the code under the dissembler ILDSAM and found that the property of the second structure gets implemented as a public function in MSIL.

Dissembled code

The fact is that the struct can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all the fields are initialized.

In .NET, all simple types are structures so when you write code in C#, consider the case below where I am creating an integer variable:

int a;
Console.WriteLine(a);

The compiler raises an error that you cannot use a variable without initializing it. So you need to write either:

Default constructor assigns the value 0 to myInt

int a =0;

or

Use the new operator to call the default constructor and assign the default value to the variable

int a = newint();

Summary

It's very important to initialize a structure properly.

Checked operator

In the following section, I am going to explain about the Checked operator available in C# .NET to handle integer overflows.

Problem

In my order management system, I have to calculate the sales point for each customer who places an order. Sales points are integer values which get earned by the customer based on the products they purchase and gets deducted from the account as they purchase new products using these points. But there are some customers who are not aware of the point system or do not consume these points so that the points accumulate to more than the limit of integer variables, i.e., 2^32. So whenever a calculation takes place, I receive some junk value for those customers who have point values more than the maximum allowed integer value.

Solution

To avoid this issue of overflow of integer values and to inform customers about their points, I use the Checked operator of C# .NET.

The Checked operator is for checking for overflow in Mathematical operations and conversions for integer types.

Example

Exception occurred: System.OverflowException: An exception of type
System.OverflowException was thrown.

which tells that c is more than the maximum allowed integer value.

The same way, in my application, I catch the overflow exception thrown when calculating order point,s and then send a mail to the customer reminding them to utilise the points they have, and letting them know they will lose the points after an xyz period and display the point as maxvalue+.

Go To with Switch.. Case

Go To

Allows us to jump unconditionally when required and is not recommended to be used heavily.

Switch..Case

Allows to execute a Case block based on the value of a variable, i.e., allows to do condition based programming.

Problem

In my application, I reached a stage where I had to execute code of Case 1 of a Switch..Case and if some condition was satisfied, I had to execute the code for Case 3 of the Switch..Case.

Execution of the statement list in the selected section begins with the first statement and proceeds through the statement list, typically until a jump statement is reached, such as a break, goto case, return, or throw. At that point, control is transferred outside the switch statement or to another case label.
Unlike C++, C# does not allow execution to continue from one switch section to the next.

Very nice Article Pranay!!! This is the first time I went through your article in Code project and Im impressed and went through your profile too (in stackflow) . Your profile is as good as your article Keep rocking..