Important Feature of C#
Introduction
In this article, I am going to discuss about some of the important things which might be unknown to most of the developers. I am going to discuss about three different topics which are related to C# and more helpful when building your application.
- Structure Initialization in C#
- Checked Operator
- Go To with
Switch
...Case
I am going to discuss the above list one by one as follows.
Structure Initialization in C#
Structure in C# allows us to group the variables and methods. Its somewhat similar to classes, but that's not true. There are a number of differences between class and structure. But here in this post, I am not going to discuss about that. Here, I am going to explain how to initialize Structure.
Facts
- Structure is Value type
- Doesn't allows to create parameter less constructor because it initializes the variable with the default values
Now consider the below case where I have created two structures.
Structure 1: with the public
members
public struct StructMember
{
public int a;
public int b;
}
Structure 2: with the properties
public struct StructProperties
{
private int a;
private int b;
public int A
{
get
{ return a; }
set
{ a = value; }
}
public int B
{
get
{ return b; }
set
{ b = value; }
}
}
Now by considering the above two facts, I tried to use both the structures as below:
public class MainClass
{
public static void Main()
{
StructMembers MembersStruct;
StructProperties PropertiesStruct;
MembersStruct.X = 100;
MembersStruct.Y = 200;
PropertiesStruct.X = 100;
PropertiesStruct.Y = 200;
}
}
After doing this, when I tried to compile the code, I received the following error.
The C# compiler issues the following error:
error CS0165: Use of unassigned local variable 'PropertiesStruct'
So by this, the C# compiler informs that it allows to use the first structure without an error but does not allow to use the second structure which exposed 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 compiled the code, it gets complied successfully.
To find out the reason why it worked without error when I wrote the second line, I reviewed the code under dissembler ILDSAM and found that the property of the second structure gets implemented as 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. So this is why we need to use New
operator to initialize.
In .NET, all simple types are ssStructures so when you write code in C#, consider the case below where I am creating one integer variable:
int a;
Console.WriteLine(a);
then compiler raises an error that you cannot use a variable without initializing it.
So you need to write either:
Default constructor assigned the value 0 to myInt.
int a =0;
or
Using the new operator calls the default constructor of the specific type and assigns the default value to the variable.
int a = new int();
Summary
It's very important to initialize structure proper way when you are coding by making use of structure.
Checked Operator
In the following section, I am going to explain about the Checked Operator available in C#.NET to handle the integer overflow.
Problem
In my order management system, I have to calculate sales point for the each customer who places an order. This sales points are integer values which get earned by customer based on the product they purchase and get deducted from the account as they purchase new product using these points. But there are some customers who are not aware of point system or not consuming these points so that the value of point gets increased more than the limit of the integer values i.e., 2^32 . So whenever calculation takes place, I receive some junk value for those customers who are having point value more than integer value, i.e. 2^32.
Solution
To avoid this issue of overflow of integer value and to inform customer who is having more points, I came across a Checked Operator of C#.NET.
Checked Operator for Checking for Overflow in Mathematical Operations and conversions for the integer types.
Syntax
Checked( expression )
ss or
Checked { statements...... }
Example
public static void Main()
{
int a;
int b;
int c;
a = 2000000000;
b = 2000000000;
c = checked(a+ b);
System.Console.WriteLine(Int1PlusInt2);
}
When we run the above code, it throws an exception as below:
Exception occurred: System.OverflowException: An exception of type
System.OverflowException was thrown.
which tells that c
value is more than the integer type length.
Same way in my application, I catch the overflow exception when thrown when calculating the order points and then raise mail to customer that you have to utilise the point you have, otherwise you will lose the points after end of xyz period and display point as maxvalue+
.
Go To with Switch.. Case
Go To
Allows us to jump unconditionally when required and it's not recommended to use Go To heavily.
Switch..Case
Statement allows to execute the case
block based on the value of the variable or passed in switch
, i.e., allow to do the condition base programming.
Problem
In my application, I reach stage where I have to execute code of the Case 1 of the Switch
..Case
and if some condition is satisfied, I have to execute the code of Case 3 of Switch
..Case
.
Example
Switch(myvariable)
{
case 1:
//statements
…........
if ( expression )
execute case 3:
break;
case 2:
…..
break;
case 3:
…......
break;
}
Solution
First solution to this problem is copy the code of case 3 and put it in the if
block of case 1.
Example
case 1:
//statements
…........
if ( expression )
//code of case 3
break;
But the problem with the above solution is that it makes code redundant.
Second solution is to create a function and put the code in that and then execute the code.
case 1:
//statements
…........
if ( expression )
Case3Code();
break;
function Case3Code()
{
….
}
The problem with this solution is that I have to create one Extra function which is not needed.
Third solution is to make use of Go To in switch
..case
block.
switch(MyVariable)
{
case 1:
//statements
…........
if ( expression )
goto case 3:
break;
case 2:
…..
break;
case 3:
…......
break;
}
So Go to in Switch
..Case
allows me to do the thing easily and in a maintainable way.
Summary
I hope you enjoyed these features discussed by me over here. I am going to continue this series as I get new things to add to this list.
History
- 16th February, 2011: Initial version
发表评论
VeEN70 I appreciate you sharing this blog. Want more.
By96Ic I appreciate you sharing this article post.Really looking forward to read more. Much obliged.