Interface Interface represents a contract. A set of public methods any implementing class has to have. Provides specification rather than an implementation for its members. Interface members are all implicitly abstract A class or struct can implement multiple interfaces Interface can contain methods, properties, events and indexers Interface members are always implicitly public and cannot […]
C# – Structs
Structs Struct is like a class but is a value type and it does not support inheritance. Useful when value-type semantics are desirable. You do not need to define a parameterless constructor but if you do define than you must explictly assign every field. Struct cannot have: Parameterless constructor Field initializers A finalizer Virtual or […]
C# – The Object Type
The Object Type System.Object is the base class for all types. Any type can be upcast to object. Object is a reference type, however you can cast between value type and object with the process of boxing and unboxing. Boxing and Unboxing Boxing Converting value-type instance to a reference-type instance (object class or interface). Copies […]
C# – Base keyword
The base Keyword Accessing members of the base class from within a derived class. Calling a base-class constructor when creating instance of the derived class. With base keyword we access base class property non-virtually. With base we call a method on the base class that has been overridden by another method. base class is permitted […]
C# – Sealed
Sealed Sealed can be applied to class and class methods. Sealed methods cannot be overridden and sealed class cannot be inherited. static void Main() { House mansion = new House { Name=”McMansion”, Mortgage=250000 }; Console.WriteLine (mansion.Liability); // 250000 } public class Asset { public string Name; public virtual decimal Liability => 0; // Virtual } […]
C# – New vs Override
New vs Override Override is bottom up approach, if the method is re-implemented in the subclass than the compiler will always use the subclass method. Even if you call the base class method the compiler will first pick the method from derived class. New will basically select the method from subclass if there is the […]
C# – Casting and Reference Conversions
Casting and Reference Conversions An object reference can be implicitly upcast to base class or explicitly downcast to subclass. Upcasting Create base class reference from a subclass reference. After the upcast the variable will reference the subclass variable. The difference is that the object will have restrictive views on properties. Example Stock fields and properties […]
C# – Polymorphism
Polymorphism Polymorphism is multiple forms of something. It can be achieved with operator overloading, method overloading, polymorphism via inheritance or polymorphism via interfaces. Polymorphism has the ability for classes to provide different implementations of methods that are called through the same name. It allows you to invoke methods of derived class through base class reference […]
C# – Partial Types and Methods
Partial Types and Methods Each participant must have the partial declaration. Participants cannot have conflicting members. A constructor with the same parameters, for instance, cannot be repeated. Partial methods A partial type may contain partial methods. These let an auto-generated partial type provide customization hooks for manual authoring. A partial method consists of two parts: […]
C# – Finalizers
Finalizers Finalizers are class-only methods that execute before the garbage collector reclaims the memory for an unreferenced object. Unmanaged code modifier: unsafe