OOPs

Constant ReadOnly and Static in C# with real life examples

Whether I should use Constant or ReadOnly or Static Variable, how to decide it, what are the reasons to choose Constant, ReadOnly or Static. It depends on the situation, usability and location to access. We are going to discuss each of these one by one and compare and check why we can use Constant and not the ReadOnly or Static variable. Why to use ReadOnly and not the Constant or Static variable... By Ali Adravi   On 03 Apr 2015  Viewed: 7,657
  • constact
  • readonly
  • static
  • c#
  • oops

Static Class in C# why and when with examples

Static class, static method and static constructor are some common questions which are asked in most interviews. A static class is basically the same as a non-static class, but there in one difference, a static class cannot be instantiated, means we cannot use new keyword to create an object of the class. Even we cannot create any parameterized constructor because to pass the constructor value we... By Ali Adravi   On 01 Jan 2015  Viewed: 23,909
  • oops
  • c#

Abstract Class in C# with Example

The purpose of an abstract class is to provide a common definition of base class that multiple derived classes can share, and can be used only as a base class and never want to create the object of this class. Any class can be converted into abstract class by adding abstract modifier to it. Common features of an abstract class: 1. An abstract class cannot be instantiated. 2. An abstract class... By Ali Adravi   On 21 Aug 2013  Viewed: 18,784
  • c#
  • asp.net
  • oops

Interface in C# with real time example

In this article we will discuss about interface in C# with real time example, it properties, implementations and pros and cons as well. 1. An interface can never be instantiated e.g. ICar car = new ICar(); It’s wrong. 2. It only contains signature of its methods. 3. An interface has neither constructor nor fields. 4. It is also not permitted to have access modifier on its methods. 5. Its... By Ali Adravi   On 18 Aug 2013  Viewed: 47,868
  • interface
  • c#
  • oops

Method overloading in C# with example

Method overloading means having different methods with same name but with different types of parameters or number of parameters also known as static polymorphism. In this article we will try to understand what is method overloading and how we can overload a method with example. Suppose we have a class Calculate which can have a method Add by using two integers, two double, any number of... By Hamden   On 19 Jul 2013  Viewed: 3,149
  • overloading
  • c#
  • oops

Polymorphism in C# with example

Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism. When we derive a class from base class it inherits all the method, properties, fields and events of the base... By Hamden   On 01 Jun 2013  Viewed: 4,571
  • c#
  • polymorphism
  • oops

Convert string to an enum type in .net

Sometime we need to convert string to an enum type, say I need to store my enum value into viewstate then we cannot store enum or any object to viewstate so converted that to string and store it, and on postback need to read the viewstate and convert to enum, so how to convert a string into enum? We will see this in this article. Let’s say our enum is like this: Public enum ReportType ... By Hamden   On 21 May 2013  Viewed: 1,558
  • enum
  • c#
  • asp.net
  • oops

Overloading and Overriding in C# with example

In my early day career in many interviews I asked same question, what is overloading and overriding so decided to write this small article which may help someone. We will see here in detail about the overloading, overriding and then differences between them. --- **Overloading:** --- is the mechanism to have more than one method with same name but with different signature (parameters). A... By Hamden   On 31 Mar 2013  Viewed: 34,644
  • overloading
  • c#
  • oops

IEnumerable and IEnumerator in C# with real time example

We will discuss here what is IEnumerable, IEnumerator, what are differences between them, where should we use IEnumerable and where to IEnumerator, once we will finish our discussion it will be clear, which one would be best for which situation and why, so let’s see it with example To better understand we will create a list of age List<int> ages = new List<int>(); ages.Add(10); ... By Ali Adravi   On 29 Mar 2013  Viewed: 44,655
  • oops
  • c#

Covariance and Contravariance in C# 4.0 with example

C# the language, right from the very beginning is always supported covariance and contravariance but in most simple scenario, now in C# 4.0 we have full support of covariance and contravariance in all circumstances, generic interface and generic delegates, other C# types already support from first version of C#. These two terms sounds like technically scary as we will see they are incredibly... By Ali Adravi   On 29 Mar 2013  Viewed: 4,994
  • c#
  • oops

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement, how to convert a text to enum or number to enum to compare, we will discuss here all these points in... By Hamden   On 27 Mar 2013  Viewed: 4,586
  • c#
  • oops

Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and strong type checking, with static type checking the compiler will produce an error for any call that... By Nathan Armour   On 26 Mar 2013  Viewed: 3,223
  • c#
  • oops

Boxing and Unboxing in C#

We know that C# data types are divided into two types, value type and reference type 1. Value types, which are allocated on the stack and 2. Reference types, which are allocated on the heap How does this square with the ability to call methods on an int, which is a value on the stack? The way C# achieves this is through a bit of magic called boxing. Boxing and its counterpart... By Ali Adravi   On 27 Dec 2012  Viewed: 1,177
  • c#
  • oops