C# – Constant

Constant A constant is a static field whose value can never change. A constant can be any of the built-in numeric types, bool, char, string, or an enum type. A constant is declared with the const keyword and must be initialized with a value. Constants can also be declared local to a method. public class […]

C# – Indexers

Indexers With indexers you can access elements in a class or struct that encapsulate a list or dictionary of values. Accessed via index argument. Example: String class has an indexer. string s = “hello”; Console.WriteLine (s[0]); // ‘h’ Console.WriteLine (s[3]); // ‘l’ Can be called on null-conditional string s = null; Console.WriteLine (s?[0]); // Writes […]

C# – Abstract

Abstract Abstract Class A class declared as abstract can never be instantiated. Instead, only its concrete subclasses can be instantiated. Specifies method signatures but lacks implementation. It is used as a template for derived classes(base class). Abstract class can have abstract and virtual members. The subclass must provide implementation unless declared abstract Link to Example […]

C# – Inheritance modifiers

Inheritance modifiers Virtual Has default implementation in the base class. Virtual method is overridden by sub classes in order to provide specialized implementation. Methods, properties, indexers, and events can all be declared virtual. Subclass override a virtual method by applying the override modifier. The signatures, return types, and accessibility of the virtual and overridden methods […]

C# – Inheritance

Inheritance It can inherit from another class to extend or customize the original class. It allows you to reuse the functionality from the base class. A class can inherit from single class but it can be inherited by many classes. Inheritance not only promotes code reuse but also helps to organize code into hierarchical structures. […]

C# – Properties

Properties Similar to fields but give more control to getting and setting value without exposing internal details to the user property. Get accessor runs when property is read, returns a value. Set accessor is to assign to property, and has implicit parameter named value. public class Stock { decimal currentPrice; // The private “backing” field […]

C# – this Reference

this Reference The this keyword refers to the current instance of a class and it is also used as a modifier of the first parameter of an extension method. public class Panda { public Panda Mate; public void Marry (Panda partner) { Mate = partner; partner.Mate = this; } } The this reference is valid […]

C# – Object Initializers

Object Initializers Any accessible fields or properties of an object can be set via an object initializer directly after construction. public class Bunny { public string Name; public bool LikesCarrots; public bool LikesHumans; public Bunny () {} public Bunny (string n) { Name = n; } } // Note parameterless constructors can omit empty parentheses […]

C# – Constructors

Constructors Constructors initialize code on class or struct. Defined like methods except method name is same as class name with no return type defined. It initializes the data member of the new object. //class public class Panda { //define field string name; //constructor with the parameter defined public Panda (string n) { //initialization code and […]

C#- Methods

Methods Receives input from the parameters and output back using the return type. Can have void return type where it doesn’t return any value Method signature must be unique within the type Modifiers Static : static Access modifiers: public, internal, private, protected Inheritance modifiers: new, virtual, abstract, override, sealed Partial: partial Unmanaged: unsage, extern Asynchronous: […]