Category: C#

anonymous class

C# allows anonymous classes in which a class can be declared inside main and initialized without specifying the data types of the class variables. On compilation, the following variable “captain” will be converted into a class: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace ConsoleApplication6 { class Program { static

Readonly fields

Readonly fields behave like constants with one difference. A readonly field can be initialized on declaration, or can be initialized only once in a constructor, and no where else. In the following example, a readonly field is initialized: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace ConsoleApplication6 { class Program

calling constructor from another constructor

In this example, we are calling no argument constructor from a two argument constructor and a two argument constructor from a one argument constructor using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace ConsoleApplication6 { class Program { public Program() { Console.WriteLine("this is no argument"); } public Program(int feet) : this(feet,40)

static constructors

Static constructor is used to initialize static variables defined in a class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication6 { class Program { static int num; int a = 10; static Program() { num = 10; // a = 40; this is error } public Program() { } public Program(

value type and ref type

In C#, classes and arrays are always passed with reference, i.e., their address is passed instead of value (String class is an exception). The variables of primitive data types, such as int, float, double, etc are passed with values. The following example demonstrates the difference between value type and reference type: using System; using System.Collections.Generic;

C# properties

C# allows properties definition. We can use a property to provide a read only, write only, or read/write access of class variables. The following is example of read/write, read, and write access of a variable named “firstname” using properties: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication6 { class Program {

C# out and ref keywords

In C# we can pass a variable to a function by address, which means, instead of passing value of a variable, we can pass its address. To do so, we can use out, and ref keywords. The different between ref and out is that, the variable passed with ref keyword must be initialized before passing

C# optional arguments

C# allows us to limit the number of arguments passed to a function but using optional arguments in function definition. The optional arguments assign a default value for the argument that is not passed in the function call. As per rule, the required arguments should come first, and then the optional arguments can be used.

C# named arguments vs fixed arguments

In C# we can specify argument name in function call. The name of the arguments should be exactly same to the name of the receiving arguments in function definition. If named arguments are used along fixed arguments, the fixed arguments should appear first, and then named arguments will appear in function call and definition. Here

Method overloading in C#

In method overloading, we can have different number of function arguments with different data types, however, the function name must be same. Moreover, the return type of function does not count in method overloading The following example demonstrates how we can perform method overloading in C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using