Scope: Scope of this article is upto its Title and motive to explain the C# Operators within minimum time and explaining with practical scenario.
The following table gives the brief overview of operators:for more details see Table:C#:Operators
Following table describes the C#:Operators:
Category | Operator | Arithmetic | + - * / % | Logical | & | ^ ~ && || ! | String concatenation | + | Increment and Decrement | ++ -- | Bit shifting | << >> | Comparison | == != < > <= >= | Assignmnet | = += -= *= /= %= &= |= ^= <<= >>= | Member access | . | Indexing | [] | Cast | () | Conditional or Ternary operator | ?: | Delegate Concatenation and removal | + - | Creation of Object | new | Type information | sizeof is typeof as | Overflow excetion control | checked unchecked | Null coalescing operator [new in .net2.0] | ?? |
The is and as operators The is operator allows, whether an object is compatible with a specific type.
e.g. int i=10; if ( i is object) Console.WriteLine("i is an Object");
In above, message always display because all C# data types inherits from object.
The as operator is used to perform explicit type conversion of reference type. If the type being converted is compatible with the specific type, conversion is performed successfully. However, if the types are incompatible, the as operator returns the null value
Take a look on following code:
/* This Example is a part of different * examples shown in Book: * C#2005 Beginners: A Step Ahead * Written by: Gaurav Arora * Reach at : gaurav.aroraose@yahoo.co.in*/
// File name : isasoperator.cs
using System;
namespace CSharp.AStepAhead.isasoperator {
class isasoperator { static void Main() {
int i = 10; if (i is object) Console.WriteLine("i is am object and contains {0}", i);
object o1 = "This is object1"; object o2 = 5; Console.WriteLine("Actual object values"); Console.WriteLine("o1 : {0} and o2 : {1}", o1, o2);
string str1 = o1 as string; //convert o1 to string string str2 = o2 as string; //convert o2 to string
Console.WriteLine("After explicitly conversion using 'as' operator"); Console.WriteLine("str1 : {0} and str2 : {1}",str1,str2);
} } }
For more details, visit http://www.msdotnetheaven.com/ChaptersCsharp/csharplanguagei.htm
|
No responses found. Be the first to respond and make money from revenue sharing program.
|