Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Article

C# Language Infrequent Methods, Operators and Techniques

Rate me:
Please Sign up or sign in to vote.
4.20/5 (21 votes)
4 May 2007CPOL3 min read 35.1K   29   11
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.

Introduction

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:

C#
byte b = 255; 
b++; 
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:

C#
byte b = 255; 
checked 
{ 
b++; 
} 

Console.WriteLine (b.ToString ());

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

C#
Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an
Overflow.
   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:

C#
byte b = 255;
unchecked
{ 
b++;
} 

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:

C#
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:

C#
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:

C#
int? a =null; 
int b; 
b=a??10; // b has value 10 
a=5; 
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.

C#
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:

C#
#if DEBUG && RELEASE 
#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:

C#
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...

C#
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.

History

  • 4th May, 2007: Initial post

License

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


Written By
Technical Lead Aristocrat Technologies India Pvt. Ltd.
India India
I am MCAD certified ,working with Aristocrat Technologies(www.aristocratgaming.com).

Comments and Discussions

 
GeneralNot really obscure [modified] Pin
Andrew Phillips9-May-07 4:17
Andrew Phillips9-May-07 4:17 
GeneralRe: Not really obscure Pin
vivekgaur9-May-07 4:29
vivekgaur9-May-07 4:29 
GeneralGood article Pin
roman.wagner8-May-07 20:42
roman.wagner8-May-07 20:42 
General?? operator Pin
Sebrell8-May-07 1:05
Sebrell8-May-07 1:05 
GeneralNever knew about the ?? operator Pin
Rei Miyasaka4-May-07 12:00
Rei Miyasaka4-May-07 12:00 
GeneralRe: Never knew about the ?? operator Pin
vivekgaur6-May-07 2:58
vivekgaur6-May-07 2:58 
GeneralExcellent Pin
merlin9814-May-07 7:37
professionalmerlin9814-May-07 7:37 
GeneralMisleading Pin
J4amieC4-May-07 6:17
J4amieC4-May-07 6:17 
GeneralRe: Misleading Pin
vivekgaur6-May-07 3:05
vivekgaur6-May-07 3:05 
GeneralExcellent!! Pin
chaiguy13374-May-07 5:24
chaiguy13374-May-07 5:24 
Thanks for the helpful article! I had no idea many of these existed, and will especially make use of Nullable and the ?, ?? operators.

Very much appreciated.

Logan1337
GeneralRe: Excellent!! Pin
vivekgaur6-May-07 2:59
vivekgaur6-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.