Click here to Skip to main content
13,560,290 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 4 May 2007
Licenced CPOL

C# Language Infrequent Methods, Operators and Techniques

, 4 May 2007
Rate this:
Please Sign up or sign in to vote.
Here I provide a basic overview of some things which are included in .NET 2.0/1.1 but only few developers are using them. I am not saying that nobody knows about these functions, Operators or facilities but only few developers are using these based on their requirements.


Since the last two years, I have been working in various .NET technologies as a software developer. I have worked on both .Net 1.1 & .NET 2.0. Recently, we migrated our projects from .NET 1.1 to 2.0. There are lots of new facilities provided by Visual Studio 2005. Here I provide a basic overview of some things which are included in .NET 2.0/1.1 but only few developers are using them. I am not saying that nobody knows about these functions, Operators or facilities but only few developers are using these according to their requirement.

The Checked and Unchecked Operators

Consider the following code:

byte b = 255; 
Console.WriteLine (b.ToString ());

The byte data type can only hold values in the range zero to 255, so incrementing the value of b causes an overflow. How the CLR handles this depends on a number of issues, including compiler options, so whenever there's a risk of an unintentional overflow, you need some way of making sure that you get the result you want.

To do this, C# provides the checked and unchecked operators. If you mark a block of code as checked, the CLR will enforce overflow checking, and throw an OverflowException if an overflow occurs. Let's change the code to include the checked operator:

byte b = 255; 

Console.WriteLine (b.ToString ());

When you try to run this code, you will get an error message like this:

Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an
   at Demo.DemoCSharp.OverflowTest.Main(String[] args)

If you want to suppress the overflow checking, you can mark the code as unchecked as shown below:

byte b = 255;

Console.WriteLine (b.ToString ()); 

In this case, you will lose the data but no exception will arise.

The Nullable Types and Operators

In C# 2005, due to generics, it is now possible to create nullable value types using System.Nullable<T>.

To create a nullable type of int, we can use this syntax:

System.Nullable<int> x = new System.Nullable<int>;

There is a new type modifier that you can use to create type as nullable in following way:

int? a =null;
int? b= a + 4;
int?c = a +5; 

The Null Coalescing Operator

The null coalescing operator (??) provides a shorthand mechanism to handle the possibility of null values when working with nullable & reference types. The operator is placed between two operands - the first operand must be a nullable type or reference type and the second operand must be of the same type as the first or of the type that is implicitly convertible to the type of the first operand. It evaluate as: if the first operand is not null, then the overall expression has the value of the first operand, however if the first operand is null, the overall expression has the value of the second operand. For example:

int? a =null; 
int b; 
b=a??10; // b has value 10 
b=a??10; //b has value 5 

The As Operator

This operator is used to perform explicit type conversion of reference types. If the type being converted is compatible with specified type, conversion is performed successfully otherwise this operator returns null value.

object o1 ="my string"; 
object o2 =25; 
string str1= o1 as string; //str1="my string" 
string str2 = o2 as string; //str2=null 

Preprocessor #warning and #error

These will respectively cause a warning or an error to be raised when the compiler encounters them.

For example:

#error "You have defined DEBUG & RELEASE simultaneously." 
#end if 
#warning "Don't forget to remove this line before setup." 
Console.WriteLine(" Some message"); 

The ReferenceEquals() Method

ReferenceEquals() is a static method that tests whether two references refer to the same instance of a class; specifically whether the two references contain the same address in memory. As a static method, it is not possible to override, so the System.Object implementation is what you always have. ReferenceEquals() will always return true if supplied with two references that refer to the same object instance, and false otherwise. It does, however, consider null to be equal to null:

SomeClass x, y;
x = new SomeClass(); 
y = new SomeClass(); 
bool B1 = ReferenceEquals(null, null); // returns true 
bool B2 = ReferenceEquals(null,x);     // returns false 
bool B3 = ReferenceEquals(x, y);       // returns false because x and y 
                                       // point to different objects 

Another point is that ReferenceEquals() always returns false when applied to value types, because to call this method, the value types will need to be boxed into objects. Even if you write...

bool b = ReferenceEquals(v,v);         // v is a variable of some value type 

... you will still get the answer of false because v will be boxed separately when converting each parameter, which means you get different references. Calling ReferenceEquals() to compare value types doesn't really make much sense.


  • 4th May, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead Aristocrat Technologies India Pvt. Ltd.
India India
I am MCAD certified ,working with Aristocrat Technologies(

You may also be interested in...


Comments and Discussions

GeneralNot really obscure [modified] Pin
Andrew Phillips9-May-07 4:17
memberAndrew Phillips9-May-07 4:17 
GeneralRe: Not really obscure Pin
vivekgaur9-May-07 4:29
membervivekgaur9-May-07 4:29 
GeneralGood article Pin
roman.wagner8-May-07 20:42
memberroman.wagner8-May-07 20:42 
General?? operator Pin
Sebrell8-May-07 1:05
memberSebrell8-May-07 1:05 
GeneralNever knew about the ?? operator Pin
reinux4-May-07 12:00
memberreinux4-May-07 12:00 
GeneralRe: Never knew about the ?? operator Pin
vivekgaur6-May-07 2:58
membervivekgaur6-May-07 2:58 
GeneralExcellent Pin
merlin9814-May-07 7:37
membermerlin9814-May-07 7:37 
GeneralMisleading Pin
J4amieC4-May-07 6:17
mvpJ4amieC4-May-07 6:17 
GeneralRe: Misleading Pin
vivekgaur6-May-07 3:05
membervivekgaur6-May-07 3:05 
GeneralExcellent!! Pin
logan13374-May-07 5:24
memberlogan13374-May-07 5:24 
GeneralRe: Excellent!! Pin
vivekgaur6-May-07 2:59
membervivekgaur6-May-07 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.180527.3 | Last Updated 4 May 2007
Article Copyright 2007 by vivekgaur
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid