Basics of C#

What is an Object?

  • An object can be considered a "thing" that can perform a set of related activities.
  • Any real entity.

For example:

  • The hand can grip something.
  • A fish in water (fish is an object).
  • An idea comes in your mind, and you implement it (the idea is an object).
  • Fruit in an orchard (fruit is an object).

What is a Class?

A class is simply a representation of a type of object. It is the blueprint / plan / template that describes the details of an object. A class is the blueprint from which the individual objects are created.
 
For example:

Take an example of a bowl in which you can bake a cake, the cake could be of different flavors so cake is my class and its flavors are objects.

Csharp1.gif

CODE:

How can you create a class and object in C#?

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         baby aisha = new baby();  
  6.         aisha.crawl();  
  7.         Console.WriteLine("i am too little so i can crawl only :-)");  
  8.         Console.ReadLine();  
  9.         aisha.food();  
  10.         Console.WriteLine("i eat mashed potatoes");  
  11.         Console.ReadLine();  
  12.     }  
  13. }  
  14.   
  15. class baby  
  16. {  
  17.     public void crawl()  
  18.     {  
  19.   
  20.     }  
  21.     public  void food()  
  22.     {  
  23.   
  24.     }  
  25.   
  26. }   

Csharp2.gif

Why method of main is static?

Static methods can't be accessed by an object, but can be accessed by the class.

Why ReadLine or Readkey?

ReadLine takes the whole line and we can zoom our window by pressing more than one key while ReadKey takes a word and back to the code window.

What is the concept of method?

All processes done by an object could be a method. A method is like a human being, my peon is my method, I called my peon and assigned some task, or remote is my method I do call remote and slow AC.

  • Data type of method.

  • Access modification.

  • Method name.

  • Method definition

  • Method declaration

  • Method call.

  • Parameters.

  • Method signature.


Csharp3.gif
Csharp4.gif

C# sum two numbers:

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         addi stat = new addi();  
  6.         stat.addition();  
  7.         Console.ReadLine();  
  8.     }  
  9. }  
  10.   
  11. class Program  
  12. {  
  13.     static void Main(string[] args)  
  14.     {  
  15.         addi stat = new addi();  
  16.         stat.addition();  
  17.         Console.ReadLine();  
  18.     }  

Output: 11

What is Method overload?

Within the same class more than one method can exist but with different parameters.

Code:

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         won stat = new won();  
  6.         stat.Add(4,2,1,2);  
  7.         Console.WriteLine(stat.Add(4,2,1,2));  
  8.         Console.ReadLine();  
  9.   
  10.     }  
  11. }  
  12.   
  13. class won  
  14. {  
  15.     public int Add(int a,int b)  
  16.   
  17.       {return a+b;}  
  18.   
  19.     public int Add(int a,int b,int c)  
  20.   
  21.        {return a+b+c;}  
  22.     public int Add(int a,int b,int c,int d)  
  23.        {return a+b+c+d;}  
  24.   

So what did you learn from overloading?

  • There could be two or more methods with the same name.

  • But with a different number of parameters of the same data type.

  • Or with a different data type of the same number of parameters.

  • There could be two or more methods with a different signature.

  • When the number of parameters is the same then the data type of the parameter is different or vice versa.

"In two different classes there could be methods with same name."

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.   
  6.         fa stats = new fa();  
  7.         stats.addition(1, 2);  
  8.         Console.WriteLine(stats.addition(1, 2));  
  9.         am probability = new am();  
  10.         probability.addition(2, 3);  
  11.         Console.WriteLine(probability.addition(2, 3));  
  12.         Console.ReadLine();  
  13.     }  
  14. }  
  15.   
  16. class fa  
  17. {  
  18.     public int addition(int a, int b)  
  19.     {  
  20.         return a + b;  
  21.   
  22.     }  
  23. }  
  24. class am  
  25. {  
  26.     public int addition(int a, int b)  
  27.     {  
  28.         return a + b;  
  29.     }  

What is Method Overriding?

Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its super-classes.

A subclass can give its own definition of methods but needs to have the same signature as the method in its super-class. This means that when overriding a method the subclass's method has to have the same name and parameter list as the super-class's overridden method.

What is Abstraction?

Sometimes there is a class which does not create objects; this class provides a definition to other classes and this is known as an abstraction.
For example:

Csharp5.gif

Consider fish is a class. In class fish there are two types of fish dolphin and sword; both have different food. An abstract class specifies that if a fish is a parent class and when the concept of inheritance is implemented;

fish says eat, after being inherited then the Dolphin says eat vegetables now I want to override the child method so we made the parent class virtual.

What is Polymorphism?

Polymorphism comes with overriding. To provide many ways to do the single thing.

Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.

What are namespaces?

Namespace is a wrapper which groups classes together, namespace within namespace can be exist.

Interface: C# can't perform multiple inheritance so we adopt the term "interface". An interface is just like a class; a class is a reference type while an interface is a value type.

An interface looks like a class, but has no implementation. An interface is equal to an abstract class but an abstract class can't perform multiple inheritances that an interface can perform.

C# code:

  1. interface IParentInterface  
  2. {  
  3.     void ParentInterfaceMethod();  
  4. }  
  5.   
  6. interface IMyInterface : IParentInterface  
  7. {  
  8.     void MethodToImplement();  
  9. }  
  10.   
  11. class InterfaceImplementer : IMyInterface  
  12. {  
  13.     static void Main()  
  14.     {  
  15.                 InterfaceImplementer iImp = new InterfaceImplementer();  
  16.         iImp.MethodToImplement();  
  17.         iImp.ParentInterfaceMethod();  
  18.     }  
  19.   
  20.     public void MethodToImplement()  
  21.     {  
  22.         Console.WriteLine("MethodToImplement() called.");  
  23.     }  
  24.    
  25.     public void ParentInterfaceMethod()  
  26.     {  
  27.         Console.WriteLine("ParentInterfaceMethod() called.");  
  28.     }  


Loops

In C# there are four loops:

  1. For

  2. While

  3. Do while

  4. Foreach

1. The For loop:

It's preferred when you know how many iterations you want, either because you know the exact amount of iterations, or because you have a variable containing the amount.

using System;

  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int number = 5;  
  8.   
  9.             for (int i = 0; i < number; i++)  
  10.                 Console.WriteLine(i);  
  11.    
  12.             Console.ReadLine();  
  13.         }  
  14.     }  


2.The While loop

The while loop simply executes a block of code as long as the condition you give it is true.

  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int number = 0;  
  8.   
  9.             while (number < 5)  
  10.             {  
  11.                 Console.WriteLine(number);  
  12.                 number = number + 1;  
  13.             }  
  14.   
  15.             Console.ReadLine();  
  16.         }  
  17.     }  

3. The do loop

  1. do  
  2. {  
  3.     Console.WriteLine(number);  
  4.     number = number + 1;  
  5. while (number < 5); 

A do while loop is helpful for viruses and for security level checking.

4. The foreach loop

A foreach loop operates on collections of items, for instance arrays or other built-in list types.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ConsoleApplication1  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList list = new ArrayList();  
  11.             list.Add("cat");  
  12.             list.Add("cow");  
  13.             list.Add("fish");  
  14.   
  15.             foreach (string name in list)  
  16.                 Console.WriteLine(name);  
  17.    
  18.             Console.ReadLine();  
  19.         }  
  20.     }  

Switch case

The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body.

  1. namespace ConsoleApplication9  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int caseSwitch = 1;  
  8.             switch (caseSwitch)  
  9.             {  
  10.                 case 1:  
  11.                     Console.WriteLine("hello");  
  12.                     break;  
  13.                 case 2:  
  14.                     Console.WriteLine("hi");  
  15.                     break;  
  16.                 default:  
  17.                     Console.WriteLine("Default case");  
  18.                     break;  
  19.   
  20.             }  
  21.   
  22.         }  
  23.     }  


 

Up Next
    Ebook Download
    View all
    Learn
    View all