Posts

Showing posts from December, 2025

Difference between Method Overriding and Method Hiding in C#

 when I started learning C# I didn't actually get what's with this new keyword which "hide" the method. method hiding name could be confusing for non-English speaker like me, I assumed that by "hiding" it actually deletes the old method and we can no longer access it, however that's not true. here is an example in which we are hiding a method and calling the parents method: namespace Test { public class T1 { public virtual void M() { System.Console.WriteLine("method from T1 class called"); } } public class T2 : T1 { public new void M() { System.Console.WriteLine("method from T2 class called"); } } public class Program { public static void Main(string[] args) { T1 obj2 = new T2(); obj2.M(); } } } The output of this C# program will be: method from T1 class called  which makes sense b...

A simple question of C#

 Hello o/ I will write a C# program and you have to predict the output of the program. namespace Test { class A { public A() { Console.WriteLine("A is called"); } } class B : A { public B() { Console.WriteLine("B is called"); } } public class Program { public static void Main(string[] args) { B obj = new B(); } } } so, what do you think it will output? the answer isn't that obvious. let me first show you the answer: A is called B is called you might be wondering why it's also printing the constructor of A? we didn't call constructor of A using base() method but still it still got called. the answer is simple, compiler adds base() call in the child.  but why??? so basically to create an instance of any class we have to have some methods defined inside System.Object which we get while creating an object. in sim...

Collections in c#

 Hello, in this blog I will talk about collections in c#. all the things written will be in my language and not in technical jargon. There are two types (There are other types of primitive arrays too which I will not cover in this blog) of collections in c#: non-generic collection: ArrayList, Stack, Queue etc... generic collection: List<T>, Stack<T>, Queue<T> etc.. Non-Generic collection: It can contain data of any type. you don't have to specify a type while creating an instance of it. It's size is dynamic means that it can grow or shrink at runtime add, remove from the middle of a collection ( which we can't do in traditional c like array ) It's not type safe since it can contain different types of objects. Generic collections: It can only contain datatype given by the developer which creating an instance of it. It's also dynamic. you can add, remove etc in the collections. It's only contains objects with specified type, so it's type safe...