C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and many more. In C#, using String and string doesn’t make any difference. both are the same except that the thing that String is a class while string is a keyword which is an alias for System.String class. Defining String: …

Read moreC# String

C# Encapsulation

Encapsulation is a mechanism in which we wrap the data into a single unit called class. By Encapsulation, Data alteration is not possible through unknown sources. The data can only be accessed through getter methods. Encapsulation increases the maintenance of the code which enhances usability. We can access the class data by the getter and setter methods only. We can’t access the data directly. Let’s see an example of …

Read moreC# Encapsulation

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private Protected Internal Protected internal Public Let’s see each one of them in detail: Access Modifier Description public Public variable is accessible everywhere Protected Protected is accessible only in derived class Internal Internal is accessible only in containing assembly …

Read moreC# Access Modifiers

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace which is used in merely every Console application. It provides methods for writing and reading from console. We can use ‘using’ keyword to avoid the use of fully qualified name to use a particular method present in a …

Read moreC# Namespace

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used to achieve full abstraction because it can’t have normal methods. In C#, multiple inheritance is achieved by interface. Interface can’t be instantiated. If a class is implementing an interface then it must give definition to all of its methods. …

Read moreC# Interface

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#, Abstraction is achieved by two ways: By abstract class By Interface Abstract class and interface both can have abstract methods which are necessary to achieve abstraction. Abstract Methods: A Method which has only declaration and no definition is called …

Read moreC# Abstraction

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed then it can’t be overridden. This is like final keyword of java. The main advantage of sealed class is that the third party vendor can’t develop any software by inheriting our logic. Sealed class is the last class in …

Read moreC# Sealed

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance, encapsulation and polymorphism. There are two types of polymorphism named as compile time polymorphism and run time polymorphism. Compile time polymorphism is achieved by method overloading and operator overloading. Runtime polymorphism is achieved by method overriding. Dynamic Method Dispatch: …

Read moreC# Polymorphism

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside instance methods and constructors. It can’t be used inside any static method like main. C# Base class example: accessing parent’s class fields

Output

C# Base Example: calling Parent class methods

Output

← Prev Next→

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides specific implementation to the method which is already defined in the base class. To perform method overriding, we need to use virtual keyword for base class method and override keyword for chilled class method. Let’s see a simple example …

Read moreC# Method Overriding

C# Member overloading

If we define two members in a class with the same name then it’s called member overloading. Advantage of member overloading is the thing that we don’t need to give different names to the members of the same meaning and are defined for the same purposes. In C#, we can overload constructors, methods and index properties since they have parameters defined in their signature. C# Method overloading: In a …

Read moreC# Member overloading

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as an entity reference. Let’s see a simple example of Aggregation where a field address of the class Employee is defined as Address class which contains all of its components. The Address class is instantiated first and then its object …

Read moreC# Aggregation

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class inherits all of its base classes. Let’s see a simple example in which a class animal is inherited further by Dog class and this is also inherited by another class babydog. At last babydog not only acquire all the …

Read moreC# Multilevel Inheritance

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object while the one which is inherited is called parent object. The chilled object can reuse, redefine all the properties and behavior of its parent object. The class which inherits another class to access its members is called derived class …

Read moreC# Inheritance

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all the fields of the class private and use only properties for getting and setting values. Prior to this, we were using method for this purpose. We can implement the logic also while setting values. Properties in C#, are read …

Read moreC# Properties

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set of constants and they can be traversed. We can define Enum outside or within the class or structs. Let’s see a simple example of enum in C#. C# Enum Example:

Output

C# Enum Example: Traversing all …

Read moreC# enum

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using structs is useful if you have data that is not intended to be modified after creation of struct. Let’s see an example of Structs

Output

C# Struct Example 2: Using Constructors

Output

← Prev Next→

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only. It is invoked automatically when a constructor is called or any static filed is referenced. We can’t apply any modifier to static constructor. It can’t be called explicitly. Let’s see an example of static constructor: C# Static Constructor Example …

Read moreC# Static Constructor

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain instance constructors. You don’t need a private constructor to avoid instantiation if your class is static. C# static class Example:

Output

← Prev Next→

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only once to a static field at the time of class loading which is common among all the objects. Advantages: We don’t need to create object to access static data that’s why it is memory efficient. The data which is …

Read moreC# static field