Intermediate C#

Skyrocket your C# skills with interfaces, generics, static access modifier and many more.

Posted by Rishi Raj Gujadhur 1/15/2016


Table of content

 

Prerequisites

 
  • A good knowledge of Easy c# concepts is required. Easy c# tutorials: Easy_C#
 
  • Microsoft Visual Studio Ultimate or Express for windows 2012 - 2015
 

A. Enums




1. Outside of the Main event , Type:

enum Department
        {
             Marketing,
             Development,
             HR         
        };

            Department empTom = Department.Development;
 
= Department.Development; ----> The employee tom as been initialise to the development department. 

Quick Tip: The Intellisence in visual studio, shows Enum type as you type on the keyboard. Just press tab and select the enum type you want. Boom! Auto complete.

2. Inside the Main event test whether the employee tom's department by typing:
 
            if (empTom == Department.Development)
            {
                Console.WriteLine("Tom is in the development department");
            }
            else if (empTom == Department.Marketing)
            {
                Console.WriteLine("Tom 's department is marketing.");
            }
            Console.ReadLine();
 
3. Execute the application.

Result: Tom is in the development department
 

B. Constant




1. Type:

const string _cs = ".cs";
 
const ----> the const keyword is followed by

the datatype of the variable,

its name

its value.
 
_cs = ""; ----> Overwriting the constant variable will produce a compile-time error:

Console.WriteLine(_cs); ----> Accessing a constant variable

2. Execute the application.

Result: .cs
 

C. Split

 

 
string sentence = "I am Batman!";          

string[] words = sentance.Split(' ');
 
The size of the array will be = to the number of words that get split.
 
all the word that the split method returns are stored in the previously declared and initialized "words" array
 
.Split(' ') ----> // Split string base on spaces between characters.

foreach (string word in words)
{
    Console.WriteLine(word);
}

Result:
I
Am
Batman!
 

D. Do While Loop




1. Type:

do
{
              
Console.WriteLine("1 is greater than 2");
Console.ReadLine();
}

 
do{ } ----> The impossible statement to be executed is enclosed in curly brackets after the do keyword.
 
while (1 > 2);
 
while (1 > 2); ----> the condition is stated afterwards.
 
2. Execute the application.
 
Result: 1 is greater than 2
 

E. Method overloading




public static void MyMethod()
{

     Console.WriteLine("no parameter");
}

 
public static void MyMethod(string name)
{

     Console.WriteLine(name);
}


In the Main event:

MyMethod();
MyMethod("Tom");

 
Result:
no parameter
Tom
 

F. Try and Catch 




1. Type

try
{

     int[] myArray = new int[3] { 1, 2, 3 };
     Console.WriteLine(myArray[4]);
     Console.ReadLine();
}

 
try{} the statement which can cause an error is enclosed within curly brackets. 

catch(Exception ex)
{

    Console.WriteLine(ex.Message);
    Console.ReadLine();
}

 
catch(Exception ex) ----> the catch method takes the exception class as parameter using which we can get information about the error that occurred.
 
ex.Message ----> get the error message in a readable format.
 

G. Generics

 


1. Type

class RocketBunny<T>
        {
            T value;
 
class RocketBunny<T> ----> When we specify a generic class the <T> characters should be added.

The T can be substitute with any alphabetical letter.

T value; ----> Notice that the datatype of the value is generics

            public RocketBunny(T parameter)
            {               
                value = parameter;
            }
 
T parameter ----> the method's parameter is also of type generic that is its data type customizable.

value = parameter; ----> The value of the variable "value" can be of any data type since both the variable "value" and the parameter being supplied are of the generic data type. 

            public void Write()
            {
                Console.WriteLine(value);
            }
        }
 
2. Type in the Main event:
              
RocketBunny<int> objPen = new RocketBunny<int>(1); 
   
RocketBunny<int> objPen = new RocketBunny<int> ----> Instantiating the generic object objPen as an integer.         

(1) ----> initialising the objPen value to 1.

objPen.Write();
           
3. Execute the application.
 

H. Static Access Modifier




1. Type:

static class Island
        {
            public static string Name { get; set; } 

            static public void Write()
            {
                Console.WriteLine(Name);
            }
        }
 
By default class are have the private access modifier for security enhancement, however we can add the public keyword in front of a class, property or method to access these outside of the class itself.

Just like the public keyword; the static keyword can be added to classes,properties and methods. The static access modifier is alone and thus it requires no instantiation.

Static access modifiers runs faster than other access modifiers however they are not very flexible. 

2. In the main event type:

static void Main(string[] args)
{

   Island.Name = "Mauritius";
 
Island.Name ----> Static properties requires no instantiation, thus we cannot have more than 1 instance of the Island class.
   
Island.Write();
Console.ReadLine();
}


Result: Mauritius

 


I. Interfaces




1. Out side of the program class, type:

interface Channelr
{
   void Write();
}
 
void Write(); ----> the class that inherites the interface ChannelR needs to have a method named Write() in it to function.

2. To create a new class inheriting from the previously created interface, type:

class myClass : Channelr
{

   public void Write()
   {
      Console.WriteLine("Hello World");
   }
}


The concept of inheritance is beyond the scope of this tutorial, however I would try covering it in the advance c# tutorial article. 

3. In this Main event, type:

Channelr objChannelR = new myClass(); 
objChannelR.Write();‚Äč
 

J. Next Step

 
  • Feel free to apply all the concepts learn in this tutorial using Microsoft Visual Studio.