Quick C# review for beginners

Few days ago I reviewed C# very quickly in few hours after two or maybe three years of being far from Visual Studio and totally Windows OS. While I was reviewing some parts of C#, I made note and I will share for those who want to have quick review and hope it will be helpful. In addition, this review is not completed, for instance, Linq part and exception handling are not covered. I will try to add more sections later. Finally, since I made this manual quickly, it is not error-free and if you find any types of mistake please report me to correct it.

1. Overloading in C# means that I write different interface for the methods that are located in the same scope and it refers to define various methods with the same name in the same class but input parameters would be different.

2. The main purpose of encapsulation is to hide difficulty and function of the class from outside view. It’s more likely to black box concept which means the functionality of the class is hidden from outside. They just use the class with creating instance(s) of it and then call methods.

In addition to that, the other use of encapsulation is to block unauthorized outside access to the class members. For example, limiting the access to a field of class.

public Point(int x, int y)
   this.x = x; //this refers to the members that have the same name outside of the method.
   this.y = y;

3. When a method is defined as static, it does not need to create instance of that class and it’s possible to call it without making an instance. But for accessing to non-static methods, first instance of that class is needed to be created. Additionally, it’s not possible to access static methods from created instances of the class.

4. In C# creating static field is also possible which then shares one variable among all instances of the class. Furthermore, creating static class also possible but all members of the static class should be static as well.

5. You create an anonymous class simply by using the new keyword and a pair of braces defining the fields and values that you want the class to contain, like this:

myAnonymousObject = new { Name = "John", Age = 44 };

6. Local variables inside of the block code is kept in Heap while global variable and variables that are used when a class is created are stored in Stack. In the case of Heap storing, the reference is kept in Stack anyway.

circle c;
c = new circle(42);
object o;
o = c; //O now points to C, which means the same reference that C refers.

7. Defining enum is like below

enum season { Spring, Summer, Fall, Winter }
season s1;

8. Definition of Struct is C# is like following two examples;

Example one:

struct Time
public int hours, minutes, seconds;

Example two:

struct Time
   public Time(int hh, int mm, int ss)
     hours = hh % 24;
     minutes = mm % 60;
     seconds = ss % 60;
   public int Hours()
     return hours;
   private int hours, minutes, seconds;

9. Defining array in C#;

int[] x=new int x[10];

10. Initializing array when defining it.

int[] rt = new int[] { 10, 20, 30 };

11. Creating random numbers:

random r = new random();
int[] pins = new int[4]{ r.Next() % 10, r.Next() % 10,
r.Next() % 10, r.Next() % 10 };

12. Defining array without type and allow C# automatically define type

var names = new[]{"John", "Diana", "James", "Francesca"};

13. Accessing to the members of the array with using For-Each

int[] pins = { 9, 3, 7, 2 };
foreach (int pin in pins)

14. Copying array in another new array without reference copying

int[] pins = { 9, 3, 7, 2 };
int[] copy = new int[pins.Length];
pins.CopyTo(copy, 0);

15. Defining Multidimensional array

int[,] items = new int[4, 6];

16. Collection classes are look like array but they do not have the limitations of the arrays. It other word collections are also type of the array but without their limitation. The following list says about when to use collection in programming.

  1. When we need to save various types of objects or basically objects in one array. For instance, String, Textbox, Button, int, double, etc. are all will be kept in one array.

  2. When we want to add, remove, shift, sort and doing other things on array.

  3. When we want to have dynamic size array without messing up with increasing array or making it small.

  4. By contrast of Array which could have multidimensional, Collections just could be linear.

In the above conditions we can use ArrayList collection. For using it, we should firstly, add following line in our code.

Using System.Collections;

And for defining a collection we could use:

arrayList arr=new ArrayList();
Arr.removeat(0); //It will remove textbox1 and will shift the rest upside.
foreach (int number in new int[12]{10, 9, 8, 7, 7, 6, 5, 10, 4, 3, 2, 1})
// insert an element in the penultimate position in the list, and move the last item up

// (the first parameter is the position;
// the second parameter is the value being inserted)
numbers.Insert(numbers.Count-1, 99);
int number = (int)numbers[i]; //Reading from ArrayList;

Now we also can define array of object which is similar to ArrayList but it does not have flexibility of the ArrayList like below.

object[] x=new object[10];

17. Defining Queue in C# with using Collections:

queue q1=new queue();
int x=(int) q1.dequeue();

18. Defining Stack in C# with using Collections:

stack s1=new stack();
int x=convert.toInt64(s1.pop());

19. HashTable is a type of collection and it’s used when we want to retrieve item of the array based on something else rather than integer (x[4]). For example like (x[“Ali”]).

In fact one reason to use hashtable is to define array but with different index value rather than ordered one like 1,2,3,4. For instance it’s useful in recommender system when rated item numbers are different such as 4,600,1000,40,67,…;

HashTable has following features:

  1. It does not have duplicate Key.

  2. It’s type of spare data type and it’s useful when data are kind of spare such as spare dataset.

Below example shows how to use HashTable in C#.

hashTable ht=new HashTable();
ht[1]=”Hello”; //1 is Key, Hello is Value;
ht[56]=123; //56 is Key, 123 is Value;
ht[“Kixz”]=”equation”; //Kixz is Key, equation is Value;
ht[“echo”]=567; //echo is Key, 567 is Value;
foreach(DictionaryEntry item in ht)
ht.add(12,”World!”); //The first one is Key and the second one is Value;

20. SortedList is very similar to HashTable and the only difference between SortedList and HashTable is that, SortedList always sorts the Keys of the list. In other word, when we get outcome of the work , the output is always sorted based on Keys (First Element/Index).

sortedList sl=new SortedList();

21. Initializing ArrayList and HashTable

arrayList numbers = new arrayList(){10, 9, 8, 7, 7, 6, 5, 10, 4, 3, 2, 1};
hashtable ages = new Hashtable(){{"John", 44}, {"Diana", 45}, {"James", 17}, {"Francesca", 15}};

22. When we want to define a method that could be able to get as many parameters as we pass to it, we could use array with params key word like below:

public int min(params int[] pp)
//code here

The issue of above code is that it just gets integers but the number of integers to get is not limited. In other word, the type is limited but the number is not limited. For overcoming with the type limitation we could use ‘object’ type like following example,

public int min(params object[] obj)
//code here

23. Inheritance in C# is possible like below:

public class inherited: BaseClass

24. It’s possible to call constructor of the base class in the inherited class like below:

class MyMain{
public MyMain(int x){}
class MySecond:MyMain{
public MySecond(int y):base(x){}

25. It’s possible to assign Inherited class to Super class or Parent class. It exactly like this that you want to put smaller bowl into bigger bowl which is possible but the opposite is not possible at all.

26. In C# if declare a method in inherited class with the same name of its parent, the compiler will give warning regarding to the issue. Therefore, the solution is to use new before defining the method in child class like below.

 new public void myth(){}

27. Virtual method means that we can implement a method of the system based on our taste. For example we can redefine ToString method from Object class differently.

namespace System
   class Object
     public virtual string ToString(){}

Now if subclass wants to redefine virtual method, it should use word “override” in method definition like below:

class subclass{
public override int mio()
base.mio(); //Calling super class, parent class method in the sub class with using ‘base’ keyword.
return 2;

28. Some hints and important notes about overriding and virtual:

  1. Override method and Virtual method could not be declared as private.

  2. Both methods should be identical (same parameters in type and numbers, plus return value).

  3. Just Virtual class can be overridden and if ‘override’ word is used on the condition that word ‘virtual’ is not used in base class compilation error will occur.

  4. Overriding is base of polymorphism.

Overall, Interface means defining a type of class that does not contain any code or fields. It just has some definition of methods not real implementation and therefore, when a class inherited an interface it could implements the methods of it but the number of parameters and type and return type should match. It exactly, like C programming that we define function names before main function to address that we use them before but we do not implement them.

interface facial{
int mio();
void pio(String s);
class testing: facial{
int mio()
{return -1;}

29. C# does not support multi inheritance but fortunately it supports inheritance from one class and one interface at the same time but it has rule. In defining subclass first name of parent class and then name of the interface and in addition they should not have anything in common. Look at the following example.

interface myface{
int iFace();
class SuperClass{
public int calc()
{return 10;}
class Inherited: SuperClass, myface
public int iFace()
return 20;
public int calc()
{return 150;}

30. Referencing to interface is exactly looks like referencing to subclass and superclass.

MySubClass msc=new MySubClass();
MyInterface mi=msc; //Correct;

31. A class can have at most one base class, but it is allowed to implement an unlimited number of interfaces . A class must still implement all the methods it inherits from all its interfaces .

32. When a class inherits from various interfaces but they have the same method with the same name, then for distinguishing them we should explicitly express which method we want to implement like below example,

class Horse : ILandBound, IJourney
int ILandBound.NumberOfLegs()
{return 4;}
int IJourney.NumberOfLegs()
return 3;

Some important note about interface:

  1. It’s not allowed to define field in interface.

  2. It’s not allowed to define constructor in interface.

  3. It’s not allowed to define destructor in interface.

  4. It’s not allowed to define public, private, protected.

  5. Interface cannot inherits from classes or structs, it could just only inherits from other interfaces.

33. When we see some classes share something in common and we see many duplication, then it’s alarm for us and we should start using abstract class instead. Abstract method are look like virtual method but it does not have code inside and it’s just definition looks like interface and when a method is defined as abstract, the sub class HAS TO override that method. We cannot create instance from abstract class and it’s only used for inheritance matter. Look at below example,

abstract class myclass: MyInterface1, MyInterface2
abstract int calc();

34. If we want to define a class that not be base class or parent class later, we could use the keyword seal when defining the class like below.

sealed class MySeal

In addition to that, it’s possible to define seal method in unseal classes like below.

35. When we are using collections we could put anything in them since the type that they are using is Object type and it can hold anything but before using it, we should cast it and if we do wrong casting we could face with run-time and/or compile error. In order to avoid such issue using generic is highly recommended to decrease the chance of making such mistakes. In fact generic removes the need of casting, increasing safety and security of the data as well as save time and memory. For defining Generic, see below example.

using System.Collections.Generic;
queue myq=new Queue();
String s=myq.Dequeue();
struct Person
dictionary directory = new Dictionary();
Person john = new Person();
directory["John"] = john;
Person author = directory["John"];

36. It’s possible to define various operator for the class to do special task like below:

class MyClass{
public static int operator *(MyClass m1,MyClass m2)
{return 40;}

Some important note about defining operators:

  1. It should define as public.

  2. It should define as static.

  3. It cannot be overridden, virtual, abstract but it could be overloaded.

  4. For ++ and – it’s also possible to define operator;

For more information about C# and tutorial please refer to official Microsoft website,

MSDN C# tutorial.

send your idea and information to kasra.madadipouya@geeksweb.eu.org

Leave a Reply