Keyword | Description |
Virtual | The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class.
public virtual double Area() { return x * y; } |
Sealed | The sealed modifier can be applied to classes, instance methods and properties. A sealed class cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. When applied to a method or property, the sealed modifier must always be used with override. |
Abstract | The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class. In this example, the class Square must provide an implementation of Area because it derives from ShapesClass: abstract class ShapesClass { abstract public int Area(); } class Square : ShapesClass { int x, y; // Not providing an Area method results // in a compile-time error. public override int Area() { return x * y; } } Abstract classes have the following features:
{ void M(); } abstract class C: I { public abstract void M(); } |
Interface | An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface, as shown in the following example: interface ISampleInterface { void SampleMethod(); } class ImplementationClass : ISampleInterface { // Explicit interface member implementation: void ISampleInterface.SampleMethod() { // Method implementation. } static void Main() { // Declare an interface instance. ISampleInterface obj = new ImplementationClass(); // Call the member. obj.SampleMethod(); } } An interface can be a member of a namespace or a class and can contain signatures of the following members:
When a base type list contains a base class and interfaces, the base class must come first in the list. A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface. |
Here, we'll learn about Session Wrapper design pattern to ease the access of Session. We'll make our access of session "Typed". Also, we may apply any validation or constraint in this wrapper class. Step 1 - Create a Session Manager class In this example, we are going to store two items in Session (i.e. ID & LoginName). We are injecting IHttpContextAccessor so that we can access the Session variable. We are creating properties which are actually accessing Session variable and returning the data or writing the data to Session. We have added one helping property "IsLoggedIn" which is using other properties to make a decision. We may have more such helping/wrapper properties. using Microsoft.AspNetCore.Http; public class SessionManager { private readonly ISession _session; private const String ID_KEY = "_ID"; private const String LOGIN_KEY = "_LoginName"; publ
Comments