C# Interfaces

Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. An interface represents a contract, in that a class that implements an interface must implement every aspect of that interface exactly as it is... » read more

C# Overriding

Overriding Members By default, a derived class inherits all members from its base class. If you want to change the behavior of the inherited member, you need to override it. That is, you can define a new implementation of the method, property or event in the derived class. The following modifiers are used to control... » read more

Sealed vs Abstract Class

Inheritance enables you to create a new class that reuses, extends, and modifies the behavior that is defined in another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. However, all classes in C# implicitly inherit from the Object class that supports .NET... » read more

Anonymous Types

Anonymous types enable you to create objects without writing a class definition for the data type. Instead, the compiler generates a class for you. The class has no usable name and contains the properties you specify in declaring the object. To create an instance of an anonymous type: // sampleObject is an instance of a... » read more

Static Classes and Members

A static member of the class is a property, procedure, or field that is shared by all instances of a class. To define a static member: static class SampleClass { public static string SampleString = "Sample String"; } Console.WriteLine(SampleClass.SampleString); Static classes in C# have static members only and cannot be instantiated. Static members also cannot... » read more

Instantiating Classes

To create an object, you need to instantiate a class, or create a class instance. SampleClass sampleObject = new SampleClass(); // Set a property value. SampleClass sampleObject = new SampleClass { FirstProperty = "A", SecondProperty = "B" };

Nested Classes

A class defined within another class is called nested. By default, the nested class is private. class Container { class Nested { // Add code here. } } To create an instance of the nested class, use the name of the container class followed by the dot and then followed by the name of the... » read more

C# Constructors

Constructors Constructors are class methods that are executed automatically when an object of a given type is created. Constructors usually initialize the data members of the new object. A constructor can run only once when a class is created. Furthermore, the code in the constructor always runs before any other code in a class. However,... » read more

Classes vs Structs

Classes Only: Can support inheritance Are reference (pointer) types The reference can be null Have memory overhead per new instance class SampleClass { } Structs Only: Cannot support inheritance Are value types Are passed by value (like integers) Cannot have a null reference (unless Nullable is used) Do not have a memory overhead per new... » read more

C# Method Overloading

In c#, Method Overloading means defining a multiple methods with same name but with different parameters. By using Method Overloading, we can perform a different tasks with same method name by passing different parameters. Suppose, if we want to overload a method in c#, then we need to define another method with same name but with different signatures. In c#,... » read more