Articles Zone
I.T DotNet
Home
IT --> DotNet --> C-Sharp
 
C-Sharp Tips - Part I
Verbatim strings can be used to represent complex strings in a simple way.Take for example file names which may contain special characters like slash(\).We have to use escape sequence to avoid the C# compiler from processing special charecters within the file name.
Example
using System;
class Test
{
static void Main()
{
string strFileName = "c:\\MyFolder\MySubFolder\\MyFile.txt";
Console.WriteLine(strFileName);
}
}


In the above example the compiler would have thrown an error if the slashes were not escaped. An easy way out is using verbatim strings.A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character.The contents of a verbatim string is not processed by the C# compiler.

Example
using System;
class Test
{
static void Main()
{
string strFileName = @"c:\MyFolderMySubFolder\MyFile.txt";
Console.WriteLine(strFileName);
}
}

Verbatim string literals can also span multiple lines.

Example
using System;
class Test
{
static void Main()
{
string S=@"
God
Is
Great";
Console.WriteLine(S);
}
}

Note:
If a verbatim string contains double quotes all the occurance of double quotes will have to be doubled.(eg) string S="He said ""Hello""";

Next TIP :

C# doesnot allow the usage of keywords for identifiers.Though using keywords for identifer names is not advisable but we may need to use them when using code from other languages.In such cases we can to prefix the keywords with @symabol and use them as identifiers
Example
using System;
class Test
{
static void Main()
{
string @string="Mystring";
}
void @void()
{
Console.WriteLine("Void Method");
}
}


In the above example I have declared a string variable by the name of @string and a method by the name of @void.

Next Tip :

The Main method is the entry point of your C# program.It is possible to have more than one Main method defined within a single C# program.In such cases we can use the /main complier option to specify which class Main method which we want to use as the entry point for the program.The syntax for using the /main option is
csc sourcefilename /main:classname

Example
using System;
class MainTest1
{
public static void Main()
{
Console.WriteLine("Main from Test Method1");
}
}
class MainTest2
{
public static void Main()
{
Console.WriteLine("Main from Test Method2");
}
}


Compiling the above program as
csc MainTest.cs /main:MainTest1
will produce the output
Main from Test Method1
and if we Compile it as
csc MainTest.cs /main:MainTest2
will produce the output
Main from Test Method2
and if /main option is not used then we will get the following compiler error.
MainTest.cs(12,21): error CS0017: Program 'MainTest.exe' has more than one entry point defined:

Next Tip :

The using keyword can be used to create an alias so that it is easier to qualify an identifier to a class or namespace.For example we use the WriteLine method of the Console class from the System namespace to print some output to the console.We can create a alias for the Console class so that we need not fully qualify it every time we use it. The syntax for creating a alias is
using alias = class or namespace
The following example shows you how to create an alias for the Console class.

Example
using Con = System.Console; //creating an alias for the Console class
class Test
{
public static void Main()
{
Con.WriteLine("Hello Universe");
}
}

In the above example the WriteLine method

Next Tip :

A C# program can span across multiple files.The syntax for compling a program that spans multiple files is
csc sourcefile1 sourcefile2 .....
By default the ouptut file will take it's name from the source file that contains the Main method.If we are creating a dll the output filename will be taken from the first source file.

Example
A.CS
------------

using System;
class A
{
public static void Main()
{
B b = new B();
b.PrintMsg();
}
}

B.CS
------------

using System;
public class B
{
public void PrintMsg()
{
Console.WriteLine("God");
}
}

Compiling the above program as
csc A.cs B.cs
will create an output file named A.exe since A.cs contains the Main method.
The /out compiler option can be used to change the name of the output file. The syntax for using the /out option is
csc /out:filename sourcefile1,sourcefile2 ...
In our example if we want our output filename to be B.exe the compilation should be done as
csc /out:B.exe A.cs B.cs

Next Tip :

Since C# doesnot allow global methods or variables ,some times we may may end up with a class containing only static members.When a class contains only static members there is no meaning in creating an instance of that class. We can prevent users from creating an instance of a class by introducing a private constructor within the class.

Example
public class Color
{
static int Red = 1;
static int Green = 2;
static int Blue = 3;
private Color()
{
}
}

When we try to create an instance of the class using
Color MyColor = new MyColor()
The following error will be generated by the C# compiler.
error CS0122: 'Color.Color()' is inaccessible due to its protection level
The members can be accessed directly using the type name (ie) class name as follows
Color.Red

Next Tip
:

Some times we may require a method which can accept variable number of arguments.C# provides the params keyword for this purporse.The syntax for using params keyword is
params datatype[] argument name
For using the params keyword he agrument must be declared as an single dimensional array.Once an argument is prefixed with the params keyword C# will accept any number of values (including none)for that argument.

Example
public class TestClass
{
public int Sum(params int[] num)
{
int totval = 0;
foreach(int n in num)
{
totval += n;
}
return totval;
}

public static int Main(string[] args)
{
TestClass T = new TestClass();

Console.WriteLine(T.Sum());
Console.WriteLine(T.Sum(3,2));
Console.WriteLine(T.Sum(50,60,100,150));
return 0;
}

}


The output of the above program will be
0
5
360
Even an array can be passed for the params argument
T.Sum(new int[]{3,2});

Next Tip:

We use explicit interface implementation to avoid ambiguity in classes implementing interfaces having members with same signature.When interface members are explicitily implemented they can be accessed only through the instance of the interface and not from the instance of the class.

Example
public class Test:MyInterface1,MyInterface2
{
void MyInterface1.MyMethod()
{
Console.WriteLine("My Method From Interface1");
}
public void MYinterface2.MyMethod()
{
Console.WriteLine("My Method From Interface2");
}
public static Main()
{
InterFace1 T1=new Test();
InterFace2 T2=new Test();
T1.MyMethod();
T2.MyMethod();
}
}

interface MyInterface1
{
void MyMethod();
}
interface MyInterface2
{
void MyMethod();
}

When try to access the method from the instance of the class the compiler will throw an error.To avoid this a forwarding method which indirectly calls to any of the explicit interface members is added to the class .

public class Test:MyInterface1,MyInterface2
{
void MyInterface1.MyMethod()
{
Console.WriteLine("My Method From Interface1");
}
public void Myinterface2.MyMethod()
{
Console.WriteLine("My Method From Interface2");
}
public void MyMethod()
{
((MyInter1)this).MyMethod();
}
public static Main()
{
InterFace1 T1=new Test();
InterFace2 T2=new Test();
T1.MyMethod();
T2.MyMethod();
}
}

interface MyInterface1
{
void MyMethod();
}
interface MyInterface2
{
void MyMethod();
}

In the above example a forwarding method is added to the class which calls to the interface memeber from MyInterface1.

Next Tip :

To specify if a WinForm should be displayed as the top-most form in our application the TopMost property is used. If the TopMost property is set to true then the WinForm is displayed as the top-most form.

Example
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
using System.Data;

public class Form1 : System.WinForms.Form
{
public static void Main(string[] args)
{
Form1 F1 = new Form1();
F1.TopMost = true;
Application.Run(F1);
}
}


Sign Guestbook View Guestbook