Tag: .net

The “is” operator

The “is operator” is used to check the type of a variable/object. The following example shows how to do it: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Person { } class Program { static void Main(string args) { int i = 10; if(i is object) { Console.WriteLine("i is

The “as” operator

“as” operator is used to cast some parent type to child type. For example, to cast from object to string, in the following example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Program { static void Main(string args) { object o1 = "Some string"; object o2 = 5; string

checked keyword

Using checked keyword, we can force the compile to throw an exception if a data type is filled with data beyond its capacity. In the following example, the maximum capacity of byte data type is 255. However, if we add more values in it, this will lead to storage of garbage value in it as

Extension methods

Extension methods are used to enhance the functionality of a class whose source code is not available. In the following example, the class Money’s functionality is extended using an extension method AddToAmount. The first argument to extension method is the class object whose functionality we want to extend and the second argument is the decimal

How to pass arguments to your C# program

To pass arguments to a C# program, all what you need to do is to run the program in command prompt and provide list of arguments in front of program name: For example: MyProgram.exe ali noman faisal shahid In above command 4 arguments as names are passed to the C# program MyProgram.exe However, sometimes you