Learning C# (Day 11) – Events In C# (A Practical Approach)


Introduction

This article of the “Diving into OOP” series will explain all about events in C#. The article focuses more on practical implementations and less on theory.

Events (The definition)

Let’s start with the definition taken from MSDN.

“Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.”

Continue reading “Learning C# (Day 11) – Events In C# (A Practical Approach)”

Learning C# (Day 10): Delegates in C# (A Practical Approach)

This article of the series “Diving into OOP” will explain all about delegates in C#. The article focuses more on practical implementations and less on theory. The article explains the concept in-depth.


Introduction

This article of the series “Diving into OOP” will explain all about delegates in C#. The article focuses more on practical implementations and less on theory. The article explains the concept in-depth.

Delegates (The definition)

Let’s start with the definition taken from MSDN

“A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.”

Continue reading “Learning C# (Day 10): Delegates in C# (A Practical Approach)”

Diving into OOP (Day 8): Indexers in C# (A Practical Approach)


Introduction

In my last article of this series we learnt about properties in c#. This article of the series “Diving into OOP” will explain all about indexers in C#, its uses and practical implementation. We’ll follow the same way of learning i.e. less theory and more practical. I’ll try to explain the concept in-depth.

Indexers in C# (The definition)

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.”

Roadmap

Let’s recall our road map,

  1. Diving in OOP (Day 1): Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism)
  2. Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
  3. Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
  4. Diving in OOP (Day 4): Polymorphism and Inheritance (All about Abstarct classes in C#)
  5. Diving in OOP (Day 5): All about access modifiers in C# (Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields)
  6. Diving in OOP (Day 6): Understanding Enum in C# (A Practical Approach)
  7. Diving into OOP (Day 7): Properties in C# (A Practical Approach)
  8. Diving into OOP (Day 8): Indexers in C# (A Practical Approach)
  9. Diving into OOP (Day 9): Understanding Events in C# (An Insight)

 

Indexers (The explanation)

Like definition says, indexers allow us to leverage the capability of accessing the class objects as an array.
For better understanding, create a console application named Indexers and add a class to it named Indexer. We’ll use this class and project to learn Indexers. Make the class public, do not add any code for now and inProgram.cs add following code,

Lab 1

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            Indexer indexer=new Indexer();
            indexer[1] = 50;
        }
    }
}
Compile the code. We get,
Error Cannot apply indexing with [] to an expression of type ‘Indexers.Indexer’
I just created an object of Indexer class and tried to use that object as an array. Since actually it was not an array, it resulted as a compile time error.

Lab 2

Indexer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexers
{
public class Indexer
{
public int this[int indexValue]
{
set
{
Console.WriteLine(I am in set : Value is “ + value + and indexValue is “ + indexValue);
Console.ReadLine();
}
}
}
}

Program.cs

namespace Indexers
{
    class Program
    {
        static void Main(string[] args)
        {
            Indexer indexer=new Indexer();
            indexer[1] = 50;
        }
    }
}

Output

Here we just made a use of indexer to index my object of the class Indexer. Now my object can be used as an array to access different object values.
Implementation of indexers is derived from a property known as “this”. It takes an integer parameter indexValue. Indexers are different from properties. In properties when we want to initialize or assign a value, the “set” accessor if defined automatically gets called. And the keyword “value” in “set” accessor was used to hold or keep track of the assigned value to our property. In above example, indexer[1] = 50;
calls the “set” accessor of “this” property i.e. an indexer therefore 50 becomes value and 1 becomes index of that value.

Lab 3

Indexer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexers
{
public class Indexer
{
public int this[int indexValue]
{
set
{
Console.WriteLine(I am in set : Value is “ + value + and indexValue is “ + indexValue);
}
get
{
Console.WriteLine(I am in get and indexValue is “ + indexValue);
return 30;
}
}
}
}

Program.cs

using System;

namespace Indexers
{
class Program
{
static void Main(string[] args)
{
Indexer indexer=new Indexer();
Console.WriteLine(indexer[1]);
Console.ReadKey();
}
}
}

Output

In the above code snippet, I used get as well, to access the value of indexer. Properties and Indexers work on same set of rules. There is a bit difference on how we use them. When we do indexer[1] that means “get” accessor is called, and when we assign some value to indexer[1] that means “set” accessor is called. While implementing indexer code we have to take care that when we access indexer it is accessed in the form of a variable and that too an array parameter.

Data-Types in Indexers

Lab 1

Indexer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexers
{
public class Indexer
{
public int Index;
public int this[string indexValue]
{
set
{
Console.WriteLine(I am in set : Value is “ + value + and indexValue is “ + indexValue);
Index = value;
}
get
{
Console.WriteLine(I am in get and indexValue is “ + indexValue);
return Index;
}
}
}
}

Program.cs

using System;

namespace Indexers
{
class Program
{
static void Main(string[] args)
{
Indexer indexer=new Indexer();
indexer[name”]=20;
Console.WriteLine(indexer[name”]);
Console.ReadKey();
}
}
}

Output

The “this” property i.e. indexers have return value. In our example the return value was integer. The square brackets along with “this” can also hold other data types and not only integer in the above mentioned example I tried to explain this using string parameter type for “this” : public int this[string indexValue],
The string parameter “indexValue” has a value “name”, like we passed in Main method of Program.cs. So one can have more than one indexers in a class deciding what should be the data type of the parameter value of array. An indexer, like properties follow same rules of inheritance and polymorphism.

Indexers in interfaces

Like Properties and Methods, Indexers can also be declared in Interfaces.
For practical implementation, just create an interface named IIndexers having following code,
namespace Indexers
{
    interface IIndexers
    {
        string this[int indexerValue] { get; set; }
    }
}
Here, an indexer is declared with an empty get and set accessor, that returns string values.
Now we need a class that implements this interface. You can define a class of your choice and implement that through IIndexers interface,

Indexer.cs

using System;

namespace Indexers
{
public class IndexerClass:IIndexers
{
readonly string[] _nameList = { AKhil”,Bob”,Shawn”,Sandra” };

public string this[int indexerValue]
{
get
{
return _nameList[indexerValue];
}
set
{
_nameList[indexerValue] = value;
}
}
}
}

The class has a default array of strings that hold names. Now we can implement interface defined indexer in this class to write our custom logic to fetch names on the base of indexerValue. Let’s call this in our main method,

Program.cs

using System;

namespace Indexers
{
class Program
{
static void Main(string[] args)
{
IIndexers iIndexer=new IndexerClass();
Console.WriteLine(iIndexer[0]);
Console.WriteLine(iIndexer[1]);
Console.WriteLine(iIndexer[2]);
Console.WriteLine(iIndexer[3]);
Console.ReadLine();

}
}
}

Run the application. Output,
In main method, we took Interface reference to create an object of IndexerClass, and we accessed that object array through indexer values like an array. It gives the names one by one.
Now if I want to access “set” accessor as well, I can easily do that. To check this, just add two more lines where you set the value in indexer,
            iIndexer[2] = "Akhil Mittal";
            Console.WriteLine(iIndexer[2]);
I set the value of 2nd element as a new name, let’s see the output,

Indexers in Abstract class

Like we used indexers in Interfaces, we can also use indexers in abstract class. I’ll use the same logic of source code that we used in interfaces, so that you can relate how it works in abstract class as well. Just define a new class that should be abstract and should contain an abstract indexer with empty get and set,

AbstractBaseClass

namespace Indexers
{
    public abstract class AbstractBaseClass
    {
        public abstract string this[int indexerValue] { get; set; }
    }
}
Define derived class, inheriting from abstract class,

IndexerClass

We here use override in indexer to override the abstract indexer declared in abstract class.
using System;

namespace Indexers
{
public class IndexerClass:AbstractBaseClass
{
readonly string[] _nameList = { AKhil”,Bob”,Shawn”,Sandra” };

public override string this[int indexerValue]
{
get
{
return _nameList[indexerValue];
}
set
{
_nameList[indexerValue] = value;
}
}
}
}

Program.cs

We’ll use reference of abstract class to create an object of Indexer class.
using System;

namespace Indexers
{
class Program
{
static void Main(string[] args)
{
AbstractBaseClass absIndexer=new IndexerClass();
Console.WriteLine(absIndexer[0]);
Console.WriteLine(absIndexer[1]);
Console.WriteLine(absIndexer[2]);
Console.WriteLine(absIndexer[3]);
absIndexer[2] = Akhil Mittal”;
Console.WriteLine(absIndexer[2]);

Console.ReadLine();

}
}
}

Output:
All of the above code is self-explanatory. You can explore more scenarios by yourself for more detailed understanding.

Indexer Overloading

Indexer.cs

using System;

namespace Indexers
{
public class Indexer
{
public int this[int indexerValue]
{
set
{
Console.WriteLine(Integer value “ + indexerValue + + value);
}
}

public int this[string indexerValue]
{
set
{
Console.WriteLine(String value “ + indexerValue + + value);
}
}

public int this[string indexerValue, int indexerintValue]
{
set
{
Console.WriteLine(String and integer value “ + indexerValue + + indexerintValue + + value);
}
}
}
}

Program.cs

using System;

namespace Indexers
{
class Program
{
static void Main(string[] args)
{
Indexer indexer=new Indexer();
indexer[1] = 30;
indexer[name”]=20;
indexer[address”,2] = 40;
Console.ReadLine();
}
}
}

Output

In above example, we see that an indexer’s signature in actually count of actual parameters and data types irresepective of the names of the arguments/parameters or return value of the indexers. This allows us to overload indexers like we do in method overloading. You can read more about method over loading inhttp://www.codeproject.com/Articles/771455/Diving-in-OOP-Day-Polymorphism-and-Inheritance-Ear. Here now we have overloaded indexers that takes integer, string integer and string combined as actual parameters. Like methods cannot be overloaded on the base of return types, so indexers follow the same methodology of overload like methods do.

Point to remember

Like indexers, we cannot overload properties. Properties are more like knowing by name and indexers on the other hand is more like knowing by signature.

Static Indexers?

In the example that we discussed in last section, just add a static keyword to the indexer signature,
      public static int this[int indexerValue]
        {
            set
            {
                Console.WriteLine("Integer value " + indexerValue + " " + value);
            }
        }
Compile the program; we get a compile time error,
Error The modifier ‘static’ is not valid for this item
The error clearly indicates that an indexer cannot be marked static. An indexer can only be a class instance member but not static, on the other hane a property can be static too.

Point to remember

Properties can be static but indexers cannot be.

Inheritance/Polymorphism in Indexers

Indexer.cs

using System;

namespace Indexers
{
public class IndexerBaseClass
{
public virtual int this[int indexerValue]
{
get
{
Console.WriteLine(Get of IndexerBaseClass; indexer value: “ + indexerValue);
return 100;
}
set
{
Console.WriteLine(Set of IndexerBaseClass; indexer value: “ + indexerValue + set value “ + value);
}

}
}
public class IndexerDerivedClass:IndexerBaseClass
{
public override int this[int indexerValue]
{
get
{
int dValue = base[indexerValue];
Console.WriteLine(Get of IndexerDerivedClass; indexer value: “ + indexerValue + dValue from base class indexer: “ + dValue);
return 500;
}
set
{
Console.WriteLine(Set of IndexerDerivedClass; indexer value: “ + indexerValue + set value “ + value);
base[indexerValue] = value;
}

}
}
}

Program.cs

using System;

namespace Indexers
{
class Program
{
static void Main(string[] args)
{
IndexerDerivedClass indexDerived=new IndexerDerivedClass();
indexDerived[2] = 300;
Console.WriteLine(indexDerived[2]);
Console.ReadLine();

}
}
}

Output

The example code taken above explains run time polymorphism and inheritance in indexers. I created a base class named IndexerBaseClass having an indexer with its own get and set like we discussed in prior examples. There after a derived class is created named IndexerDerivedClass, this derives from IndexerBaseClass and overrides “this” indexer from base class, note that base class indexer is marked virtual, so we can override it in derived class by marking it “override” in derived class.The example makes call to indexer of base class. Sometimes when we need to override code in derived class in the derived class, we may require the base class indexer should be called first. This is just a situation. The same rule of run time polymorphism applies here , we declare base class indexer and virtual and derived class one as override. In “set” accessor of derived class, we can call base class indexer as base[indexerValue]. Also this value is used to initialize the derived class indexer as well. So the value is stored in “value” keyword too. So, indexDerived[2] in Main() method of Program.cs gets replaced to base[2] in “set” accessor. Whereas In “get” accessor it is vice versa, we require to putbase[indexerValue] to right hand side of equal sign. The “get” accessor in base class returns a value, i.e. 100, which we get in dValue variable.

.NET Framework and Indexers

Indexers play a crucial role in .NET framework. Indexers are widely used in .NET Framework inbuilt classes, libraries such as collections and enumerable. Indexers are used in collections that are searchable like Dictionary, Hashtable, List, Arraylist etc.

Point to remember

Dictionary in C# largely uses indexers to have a staring parameter as an indexer argument.
Classes like ArrayList and List use indexers internally to provide functionality of arrays for fetching and using the elements.

Properties vs Indexers

I have already explained a lot about properties and indexers, to summarize, let me point to an MSDN link for better understanding,
Property Indexer
Allows methods to be called as if they were public data members. Allows elements of an internal collection of an object to be accessed by using array notation on the object itself.
Accessed through a simple name. Accessed through an index.
Can be a static or an instance member. Must be an instance member.
get accessor of a property has no parameters. A get accessor of an indexer has the same formal parameter list as the indexer.
set accessor of a property contains the implicit value parameter. A set accessor of an indexer has the same formal parameter list as the indexer, and also to the value parameter.
Supports shortened syntax with Auto-Implemented Properties (C# Programming Guide). Does not support shortened syntax.

Conclusion

With this article we completed almost all the scenarios related to an indexer. We did a lot of hands-on lab to clear our concepts. I hope my readers now know by heart about these basic concepts and will never forget them. These may also help you in cracking C# interviews.
Keep coding and enjoy reading
Also do not forget to rate/comment/like my article if it helped you by any means, this helps me to get motivated and encourages me to write more and more.

Read more:

Other Series

My other series of articles:

For more informative articles visit my Blog.

For more technical articles you can reach out to CodeTeddy.

Diving into OOP (Day 7): Properties in C# (A Practical Approach)


Introduction:

My article of the series “Diving into OOP” will explain all about properties, its uses and indexers in C#. We’ll follow the same way of learning i.e. less theory and more practical. I’ll try to explain the concept in-depth.

Properties (The definition):

Let’s start with the definition taken from MSDN
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
 

Roadmap:

Let’s recall our road map,
  1. Diving in OOP (Day 1): Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism)
  2. Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
  3. Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
  4. Diving in OOP (Day 4): Polymorphism and Inheritance (All about Abstarct classes in C#)
  5. Diving in OOP (Day 5): All about access modifiers in C# (Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields)
  6. Diving in OOP (Day 6): Understanding Enum in C# (A Practical Approach)
  7. Diving into OOP (Day 7): Properties in C# (A Practical Approach)
  8. Diving into OOP (Day 8): Indexers in C# (A Practical Approach)
  9. Diving into OOP (Day 9): Understanding Events in C# (An Insight)

Properties (The explanation):

Being a C# programmer, I must say that properties are something a C# programmer is blessed to use in his code. If we analyze properties internals, they are very different from normal variables; properties are internally methods that do not have their own memory like variables have. We can leverage property to write our custom code whenever we access a property. We can access the code when we call/execute properties or at the time of declaration too, but this is not possible with variables. A property in easy language is a class member and is encapsulated and abstracted from the end developer who is accessing the property. A property can contain lots of code that an end user does not know. An end user only cares to use that property like a variable.
Let’s start with some coding now.
Note: Each and every code snippet in this article is tried and tested.

Lab1

Create a simple console application and name it “Properties”. Add a class named “Properties” to it. You can choose the names of project and class as per your wish. Now try to create a property in that class like shown below,

Properties.cs

namespace Properties
{
    public class Properties
    {
        public string Name{}
    }
}
Try to run/build the application, what do we get?

Output

Error ‘Properties.Properties.Name’: property or indexer must have at least one accessor
In above example, we created a property named Name of type string. The error we got is very self-explanatory; it says a property must have an accessor, i.e. a get or a set. This means we need something in our property that gets accessed, whether to set the property value or get the property value, unlike variables, properties cannot be declared without an accessor.

Lab2

Let’s assign a get accessor to our property and try to access that property in Main method of program.cs file.

Properties.cs

Properties.cs:

namespace Properties
{
public class Properties
{
public string Name
{
get
{
return I am a Name property”;
}
}
}
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
Console.WriteLine(properties.Name);
Console.ReadLine();
}
}
}

Try to run/build the application, what do we get?

Output

It says “I am a Name property”. This signifies that our get successor got called when I tried to access the property or tried to fetch the value of a Property.

Get accessor

Now declare one more property in Properties.cs class, and name it Age that returns an integer. It calculates the age of a person by calculating the difference between his date of birth and current date,

Properties.cs

using System;

namespace Properties
{
public class Properties
{
public string Name
{
get
{
return I am a Name property”;
}
}

public int Age
{
get
{
DateTime dateOfBirth=new DateTime(1984,01,20);
DateTime currentDate = DateTime.Now;
int age = currentDate.Year – dateOfBirth.Year;
return age;
}
}
}
}

Call the “Age” property in the same way as done for Name.

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
Console.WriteLine(properties.Name);
Console.WriteLine(My age is “ + properties.Age);
Console.ReadLine();
}
}
}

Try to run/build the application, what do we get?

Output

It returns the correct age subjective to date of birth provided. Did you notince something here? Our property contains some code and logic to calculate age, but the caller i.e. Program.cs is not aware of the logic it only cares about using that Property. Therefore, we see that a property encapsulates and abstracts its functionality from the end user, in our case it’s a developer.

Point to remember

Get accessor is only used to read a property value. A property having only “get” cannot be set with any value from the caller.
This means a caller/end user can only access that property in read mode.

Set accessor

Let’s start with a simple example.

Lab1

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        public string Name
        {
            get { return "I am a Name property"; }
        }

public int Age
{
get
{
DateTime dateOfBirth = new DateTime(1984, 01, 20);
DateTime currentDate = DateTime.Now;
int age = currentDate.Year – dateOfBirth.Year;
Console.WriteLine(Get Age called”);
return age;
}
set
{
Console.WriteLine(Set Age called “ + value);
}
}
}
}

Call the “Age” property in the same way as done for Name.

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(My age is “ + properties.Age);
Console.ReadLine();
}
}
}

Run the application,

Output

In the above given example, I made few minor changes in get accessor, i.e. just printing that control is in “Get accessor” and introduced a “Set” in Age property too. Everything else remains same. Now when I call Name property, it works as was working earlier.Since we used “Set” so now we are allowed to set the value of a property. When I do properties.Age = 40; that means I am setting value 40 to that property. We can say a property can also be used to assign some value.In this case Set accessor is called, as soon as we set a value to property. Later on when we access that same property, again our get accessor gets called which returns value with some custom logic. We have a drawback here, as we see, whenever we call get we get the same vale and not the value that we assigned to that property i.e. because get has its custom fixed logic. Let’s try to overcome this situation.

Lab2

The example I am about to explain makes use of a private variable. But you can also use Automatic Properties to achieve same. I’ll purposely make use of variable to have a better understanding.

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        private string name;
        private int age;

public string Name
{
get { return name; }
set
{
Console.WriteLine(Set Name called “);
name = value;
}
}

public int Age
{
get { return age; }
set
{
Console.WriteLine(Set Age called “);
age = value;
}
}
}
}

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(My age is “ + properties.Age);
Console.ReadLine();
}
}
}

Run the application,

Output

Now you see, we get the same value that we assigned to Name and Age property .When we access these properties get accessor is called and it returns the same vale as we set them for. Here properties internally make use of local variable to hold and sustain the value.
In day to day programming, we normally create a Public property that can be accessed outside the class. However the variable it is using internally could be a private.

Point to remember

The variable used for property should be of same data type as the data type of the property.
In our case we used variables, name and age, they share same datatype as their respective properties do. We don’t use variables as there might be scenarios in which we do not have control over those variables, end user can change them at any point of code without maintaining the change stack. Moreover one major use of properties is user can associate some logic or action when some change on the variable occurs, therefore when we use properties, we can easily track the value changes in variable.
When using Automatic Properties, they do this internally, i.e. we don’t have to define an extra variable to do so,like shown below,

Lab3

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        private string name;
        private int age;

public string Name { get; set; }

public int Age { get; set; }
}
}

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(My age is “ + properties.Age);
Console.ReadLine();
}
}
}

Run the application,

Output

Here
    public string Name { get; set; }
    public int Age { get; set; }
are automatic properties.
I hope now you know how to define a property and use it.

Readonly

A property can be made read-only by only providing the get accessor. We do not provide a set accessor, if we do not want our property to be initialized or to be set from outside the scope of class.

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        private string name="Akhil";
        private int age=32;

public string Name
{
get { return name; }
}

public int Age { get { return age; } }
}
}

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(My age is “ + properties.Age);
Console.ReadLine();
}
}
}

Build the application, we get following output
Error Property or indexer ‘Properties.Properties.Age‘ cannot be assigned to — it is read only
Error Property or indexer ‘Properties.Properties.Name‘ cannot be assigned to — it is read only
In main method of Program class, we tried to set the value of Age and Name property by,
            properties.Name = "Akhil";
            properties.Age = 40;
But since they were marked read-only i.e. only with get accessor, we encountered a compile time error.

Write-Only

A property can also be made write-only i.e. vice versa to read-only. In this case you’ll be only allowed to set the value of the property but can’t access it because we don’t have get accessor in it.

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        private string name;
        private int age;

public string Name
{
set { name=value; }
}

public int Age { set { age = value; } }
}
}

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(My age is “ + properties.Age);
Console.ReadLine();
}
}
}

Build the application, we get following output
Error The property or indexer ‘Properties.Properties.Age’ cannot be used in this context because it lacks the get accessor
Error The property or indexer ‘Properties.Properties.Name’ cannot be used in this context because it lacks the get accessor
In the above mentioned example, our property is marked only with set accessor, but we tried to access those properties in our main program with,
            Console.WriteLine(properties.Name);
            Console.WriteLine("My age is " + properties.Age);
That means we tried to call get accessor of property which is not defined, so we again ended up in a compile time error.

Insight of Properties in C#

Lab1

Can we define properties as two different set of pieces? The answer is NO.

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        private string name;

public string Name
{
set { name=value; }
}

public string Name
{
get { return name; }
}
}
}

Build the project, we get compile time error,
Error The type ‘Properties.Properties’ already contains a definition for ‘Name’
Here I tried to create a single property segregated in two different accessor. Compile treats a property name as a single separate property, so we cannot define a property with two names having different accessor.

Lab2

Can we define properties same as an already defined variable? The answer is NO.

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        private string name;

public string name
{
set { name=value; }
get { return name; }
}
}
}

Build the project; we get compile time error,
Error The type ‘Properties.Properties’ already contains a definition for ‘name’
Again, we cannot have a variable and a property with the same name. They may differ on the grounds of case sensitivity, but they cannot share a same common name with the same case because at the time of accessing them, compiler may get confused that whether you are trying to access a property or a variable.

Properties vs Variables

It is a conception that variables are faster in execution that properties. I do not deny about this but this may not be true ion every case or can vary case to case. A property, like I explained internally executes a function/method whereas a variable uses/initializes memory when used. At times properties are not slower than variables as the property code is internally rewritten to memory access.
To summarize, MSDN explains this theory better than me,
Point of difference Variable Property
Declaration Single declaration statement Series of statements in a code block
Implementation Single storage location Executable code (property procedures)
Storage Directly associated with variable’s value Typically has internal storage not available outside the property’s containing class or moduleProperty’s value might or might not exist as a stored element 1
Executable code None Must have at least one procedure
Read and write access Read/write or read-only Read/write, read-only, or write-only
Custom actions (in addition to accepting or returning value) Not possible Can be performed as part of setting or retrieving property value

Static Properties

Like variables and methods, a property can also be marked static,

Properties.cs

using System;
namespace Properties
{
    public class Properties
    {
        public static int Age
        {
            set
            {
                Console.WriteLine("In set static property; value is " + value);
            }
            get
            {
                Console.WriteLine("In get static property");
                return 10;
            }
        }
    }
}

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties.Age = 40;
Console.WriteLine(Properties.Age);
Console.ReadLine();
}
}
}

Output

In above example, I created a static Age property. When I tried to access it, you can see it is accessed via class name, like all static members are subjected to. So properties also inherit the static functionality like all c# members, no matter it is variable or a method. They’ll be accessed via class name only.

Properties return type

Lab1

Properties.cs

using System;

namespace Properties
{
public class Properties
{
public void AbsProperty
{
get
{
Console.WriteLine(Get called”);
}
}
}
}

Compile the program.
Output is a compile time error,
Error ‘AbsProperty’: property or indexer cannot have void type

Point to remember

A property cannot have a void return type.

Lab2

Just try to return a value from “set” accessor,

Properties.cs

using System;

namespace Properties
{
public class Properties
{
public int Age
{
set { return 5; }
}
}
}

Compile the program,
Error Since ‘Properties.Properties.Age.set’ returns void, a return keyword must not be followed by an object expression
Here compiler understands “set” accessor as a method that returns void and takes a parameter to initialize the value. So set cannot be expected to return a value.
If we just leave return statement empty, and remove 5, we do not get any error and code compiles,
using System;

namespace Properties
{
public class Properties
{
public int Age
{
set { return ; }
}
}
}

Value Keyword

We have a reserved keyword named value.
using System;

namespace Properties
{
public class Properties
{
public string Name
{
set { string value; }
}
}
}

Just compile the above given code, we get a compile time error as follows,
Error A local variable named ‘value’ cannot be declared in this scope because it would give a different meaning to ‘value’, which is already used in a ‘parent or current’ scope to denote something else
This signifies that “value” is a reserved keyword here. So one cannot declare a variable named value in “set” accessor as it may give different meaning to already reserved keyword value.

Abstract Properties

Lab1

Yes, we can also have abstract properties; let’s see how it works,

Properties.cs

using System;

namespace Properties
{
public abstract class BaseClass
{
public abstract int AbsProperty { get; set; }
}

public class Properties : BaseClass
{
public override int AbsProperty
{
get
{
Console.WriteLine(Get called”);
return 10;
}
set { Console.WriteLine(set called,value is “ + value); }
}
}
}

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties prop=new Properties();
prop.AbsProperty = 40;
Console.WriteLine(prop.AbsProperty);
Console.ReadLine();
}
}
}

Output

In above example, I just created a base class named “BaseClass” and defined an abstract property namedAbsproperty. Since the property is abstract it follows the rules of being abstract as well. I inherited my “Properties” class from BaseClass and given the body to that abstract property. Since the property was abstract I have to override it in my derived class to add functionality to it. So I used override keyword in my derived class.
In base class, abstract property has no body at all, neither for “get” and nor for “set”, so we have to implement both the accessor in our derived class, like shown in “Properties” class.

Point to remember

If one do not mark property defined in derived class as override, it will by default be considered as new.

Lab2

Properties.cs

using System;

namespace Properties
{
public abstract class BaseClass
{
public abstract int AbsProperty { get; }
}

public class Properties : BaseClass
{
public override int AbsProperty
{
get
{
Console.WriteLine(Get called”);
return 10;
}
set { Console.WriteLine(set called,value is “ + value); }
}
}
}

Program.cs

using System;

namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties prop=new Properties();
prop.AbsProperty = 40;
Console.WriteLine(prop.AbsProperty);
Console.ReadLine();
}
}
}

Output

Compile time error,
Error ‘Properties.Properties.AbsProperty.set’: cannot override because ‘Properties.BaseClass.AbsProperty’ does not have an overridable set accessor
In above lab example, I just removed “set” from AbsProperty in Base class. All the code remains same. Now here we are trying to override the set accessor too in derived class, that is missing in base class, therefore compiler will not allow you to override a successor that is not declared in base class, hence resulted into a compile time error.

Point to remember

You cannot override an accessor that is not defined in a base class abstract property.

Properties in Inheritance

Just follow the given code,

Properties.cs

using System;

namespace Properties
{
public class PropertiesBaseClass
{
public int Age
{
set {}
}
}

public class PropertiesDerivedClass:PropertiesBaseClass
{
public int Age
{
get { return 32; }
}
}
}

Program.cs

namespace Properties
{
    class Program
    {
        static void Main(string[] args)
        {
            PropertiesBaseClass pBaseClass=new PropertiesBaseClass();
            pBaseClass.Age = 10;
            PropertiesDerivedClass pDerivedClass=new PropertiesDerivedClass();
            ((PropertiesBaseClass) pDerivedClass).Age = 15;
            pDerivedClass.Age = 10;
        }
    }
}
As you can see in above given code, in Properties.cs file I created two classes one is Base i.e.PropertiesBaseClass and second in Derived i.e. PropertiesDerivedClass. I purposely declared set accessor in Base class and get in Derived class for the same property name i.e. Age. Now this case may give you the feeling that when compiled, our code of property Age will become one, i.e. it will take set from Base class and get from derived class and combine it into a single entity of Age property.But this is practically not the case. The compiler treats both these properties differently, and does not consider them to be same. In this case the property in derived class actually hides the property in base class , they are not the same but independent properties.The same concept of method hiding applies here too. You can read about hiding inhttp://www.codeproject.com/Articles/774578/Diving-in-OOP-Day-Polymorphism-and-Inheritance-Dyn.
To use the property of base class from a derived class object, you need to cast it to base class and then use it.
When you compile the above code, you get a compile time error as follows,
Error Property or indexer ‘Properties.PropertiesDerivedClass.Age’ cannot be assigned to — it is read only
i.e. we can do ((PropertiesBaseClass) pDerivedClass).Age = 15;
but we cannot do pDerivedClass.Age = 10; because derived class property has no “set” accessor.

Summary

Let’s recall all the points that we have to remember,
  • The variable used for property should be of same data type as the data type of the property.
  • A property cannot have a void return type.
  • If one do not mark property defined in derived class as override, it will by default be considered as new.
  • You cannot override an accessor that is not defined in a base class abstract property.
  • Get accessor is only used to read a property value. A property having only get cannot be set with any value from the caller.

Conclusion

In this article we learnt a lot about properties in c#. I hope you now understand properties well. In my next article I’ll explain all about indexers in C#.
Keep coding and enjoy reading
Also do not forget to rate/comment/like my article if it helped you by any means, this helps me to get motivated and encourages me to write more and more.

Read more:

Other Series

My other series of articles:

For more informative articles visit my Blog.

For more technical articles you can reach out to CodeTeddy.

Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)


Introduction

My article of the series “Diving in OOP” will explain enum datatype in C#. We’ll learn by doing hands on lab and not only by theory. We’ll explore the power of enum and will cover almost every scenario in which we can use enum. We’ll follow a practical approach of learning to understand this concept. We may come across complex examples to understand the concept more deeply.

Enums (The Definition)

Let’s start with the definition taken from MSDN:

“The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.
Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.
By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.”

Pre-requisites

I expect that my readers of this article should have very basic knowledge of C# and I always wish that my readers should enjoy while reading this article. Also, keep writing programs by yourself that are given as examples in this article, to get hands on and understand the concept more deeply.

Roadmap

Let’s recall our road map.

A Practical Approach

Enum plays almost the same responsibility as the class does, i.e., creating a new data type, and it exists at the same level as class, interfaces or structs.
Just open your Visual Studio and add a console application named Enums. You’ll get Program.cs class.
Note: Each and every code snippet in this article is tried and tested.
Declare an enum at the same level as of Program class, call it as Color.

Program.cs

namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
        }
    }

enum Color
{
Yellow,
Blue,
Brown,
Green
}
}

In the above mentioned example, we created a new datatype with the help of enum. The datatype is Colorhaving four distinct values, Yellow,BlueBrown and Green. The text that we write inside the declared enum could be anything of your wish; it just provides a custom enumerated list to you.
Modify your main program as shown below:
using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine(Color.Yellow);
            Console.ReadLine();
        }
    }

enum Color
{
Yellow,
Blue,
Brown,
Green
}
}

Run the program.
 
OutputYellow
 
Now just typecast Color.Yellow to int, what do we get?
using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine((int)Color.Yellow);
            Console.ReadLine();
        }
    }

enum Color
{
Yellow,
Blue,
Brown,
Green
}
}

 
Output0
 
We see that enum is called as static variables, so an enum can be considered here as static objects. Thereforeother enums in the above example can be declared in the same way as Yellow, like Blue can be declared asColor.Blue. The output in the above two examples we see is when we typecast and Yellow without typecasting, hence we see here that its behaviour is very similar to an array where Yellow has a value 0, similarly Blue has a value 1Brown2Green3.
Therefore, when we do Color.Yellow, it’s like displaying a number 0, so from this can we infer that an enumrepresents a constant number, therefore an enum type is a distinct type having named constants.
Point to remember: An enum represents for a constant number, and an enum type is known as a distinct type having named constants.

Underlying Data type

Program.cs

using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine((byte)Color.Yellow);
            Console.WriteLine((byte)Color.Blue);
            Console.ReadLine();
        }
    }

enum Color:byte
{
Yellow,
Blue,
Brown,
Green
}
}

Output

0
1
 
Note: Each and every code snippet in this article is tried and tested.
The only change we did here is that we specified the type to the underlying enum that we declared. The defaultdatatype for the enum is int, here we have specified the data type as byte and we get the result.
There are more data types that can be specified
for enum like longulongshortushortintuint,byte andsbyte.
Point to remember: We can’t declare char as an underlying data type for enum objects because char stores Unicode characters, but enum objects data type can only be number.

Inheritance in Enum

Program.cs

using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine((byte)Color.Yellow);
            Console.WriteLine((byte)Color.Blue);
            Console.ReadLine();
        }
    }

enum Color:byte
{
Yellow,
Blue,
Brown,
Green

}

enum Shades:Color
{

}
}

Output

Compile time error: Type bytesbyteshortushortintuintlong, or ulong expected.
We clearly see here enums can’t be derived from any other type except that of mentioned in the error.
Point to rememberenum can’t be derived from any other type except that of type bytesbyteshortushort,intuintlong, or ulong.
Let’s derive a class from enum, call it class Derived, so our code.

Program.cs

class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine((byte)Color.Yellow);
            Console.WriteLine((byte)Color.Blue);
            Console.ReadLine();
        }
    }

Enum

enum Color:byte
    {
        Yellow,
        Blue,
        Brown,
        Green
    }

Derived.cs

class Derived:Color
    {

}

Compile the code.

Output

Compile time error: ‘Enums.Derived’: cannot derive from sealed type ‘Enums.Color’
Point to remember: By default, enum is a sealed class and therefore sticks to all the rules that a sealed class follows, so no class can derive from enum, i.e., a sealed type.
Can System.Enum be a base class toenum?

Program.cs

using System;

namespace Enums
{
internal enum Color: System.Enum
{
Yellow,
Blue
}

internal class Program
{
private static void Main(string[] args)
{
}
}
}

Output

Compile time error: Type bytesbyteshortushortintuintlong, or ulong expected.
Point to remember: The enum type is implicitly derived from System.Enum and so we cannot explicitly derive it from System.Enum.
To add more,enum is also derived from three interfaces IComparableIFormattable and IConvertible.

A. IComparable

Let’s check,

Program.cs

using System;

namespace Enums
{
internal enum Color
{
Yellow,
Blue,
Green
}

internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(Color.Yellow.CompareTo(Color.Blue));
Console.WriteLine(Color.Blue.CompareTo(Color.Green));
Console.WriteLine(Color.Blue.CompareTo(Color.Yellow));
Console.WriteLine(Color.Green.CompareTo(Color.Green));
Console.ReadLine();
}
}
}

Output

-1
-1
 1
 0
Sometimes, we may get into situations where we have large number of enums defined and we want to compare the values of enum to each other to check if they are smaller, larger or equal value to one another.
Since all enums implicitly derive from Enum class that implements the interface IComparable, they all have a methodCompareTo(), that we just used in the above example. The method being nonstatic has to be used through a member. Yellow has value 0, Blue as 1 and Green as 2. In the first statement, when Color.Yellow compared toColor.Blue, value of Yellow is smaller than Blue hence -1 returned, same applied for the second statement when Color.Blue compared to Color.GreenGreen has larger value, i.e., 2 than that of Color.Blue having value 1 only. In the third statement, i.e., vice versa of first statement, we get the result of camparisonas 1,becauseBlue is larger than Yellow. In the last statement where Color.Green compares to itself, we undoubtedly get the value 0.
So value -1 means the value is smaller, 1 means value is larger and 0 means equal values for both the enummembers.
Another comparison example is shown below:

Program.cs

using System;

namespace Enums
{
enum Color
{
Yellow,
Blue,
Green
}

internal class Program
{
private static void Main(string[] args)
{
int myColor = 2;
if(myColor== Color.Green)
{
Console.WriteLine(my color”);
}
Console.ReadLine();
}
}
}

Output

Compile time error : Operator '==' cannot be applied to operands of type 'int' and 'Enums.Color'
In the above example, we tried to compare an int type to Enum type and resulted in a compile time error. Sinceenum acts as an individual data type so it cannot be directly compared to an int, however, we can typecast the enumtype to int to perform comparison, like in the below example:

Program.cs

using System;

namespace Enums
{
enum Color
{
Yellow,
Blue,
Green
}

internal class Program
{
private static void Main(string[] args)
{
int myColor = 2;
if(myColor== (int)Color.Green)
{
Console.WriteLine(my color”);
}
Console.ReadLine();
}
}
}

 
Output: my color

B. IFormattable

Program.cs

using System;

namespace Enums
{
internal enum Color
{
Yellow,
Blue,
Green
}

internal class Program
{
private static void Main(string[] args)
{
System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, X”));
System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, d”));
Console.ReadLine();
}
}
}

Output

00000002
2
Format is the method derived from IFormatter interface. It’s a static method so can be used directly with theenum class defined as Color. It’sfirst parameter is the type of the enum class, second is the member that has to be formatted and third is the format, i.e., hexadecimal or decimal, like we used in the above example, and we got a positive result output too.

C. IConvertible

using System;

namespace Enums
{
enum Color
{
Yellow,
Blue,
Green
}

internal class Program
{
private static void Main(string[] args)
{
string[] names;
names = Color.GetNames(typeof (Color));
foreach (var name in names)
{
Console.WriteLine(name);
}
Console.ReadLine();
}
}
}

Output

Yellow
Blue
Green
 
Note: Each and every code snippet in this article is tried and tested.
GetNames is a static method that accepts Type, i.e., instance of type as a parameter and in return gives an array of strings. Like in the above example, we had array of 3 members in our enum, therefore their names are displayed one by one.
Another example is as follows:

Program.cs

using System;

namespace Enums
{
enum Color
{
Yellow,
Blue,
Green
}

internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(Color.Blue.ToString());
Console.WriteLine(Color.Green.ToString());
Console.ReadLine();
}
}
}

Output

Blue
Green
As we see in the above example, we converted an enum type to staring type and got an output too, so, numerous predefined conversion methods can be used to convert enum from one data type to another.
 
Point to remember: Numerous predefined conversion methods can be used to convert enum from one data type to another.
Duplicity, default values and initialization:

Program.cs

using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine((byte)Color.Yellow);
            Console.WriteLine((byte)Color.Blue);
            Console.ReadLine();
        }
    }

enum Color
{
Yellow,
Blue,
Brown,
Green,
Blue
}
}

Output

Compile time error: The type 'Enums.Color' already contains a definition for 'Blue'
In the above example, we just repeated the enum member Blue of Color, and we got a compile time error, hence we now know that an enum cannot contain two members having the same name. By default, if the first value is not specified, the first member takes the value and increments it by one to succeeding members.
Let’s take one more example.

Program.cs

using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine((int)Color.Yellow);
            Console.WriteLine((int)Color.Blue);
            Console.WriteLine((int)Color.Brown);
            Console.WriteLine((int)Color.Green);

Console.ReadLine();
}
}

enum Color
{
Yellow =2,
Blue,
Brown=9,
Green,

}
}

Output

2
3
9
10
Surprised! We can always specify the default constant value to any enum member, here we see, we specified value 2to yellow, so as per law of enum, the value of blue will be incremented by one and gets the value 3. We again specified as a default value toBrown, and so its successor Green gets incremented by one and gets that value 10.
Moving on to another example.

Program.cs

using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {

}
}

enum Color:byte
{
Yellow =300 ,
Blue,
Brown=9,
Green,
}
}

Output

Compile time error: Constant value '300' cannot be converted to a 'byte'
We just derived ourenum from byte, we know we can do that? We then changed the value of yellow from 2 to300, and we resulted in a compile time error. Since here our underlying data type was byte, so it is as simple as that, that we cannot specify the value to enum members which exceeds the range of underlying data types. The value 300is beyond the range of byte. It is similar to assigning the beyond range value to a byte data type variable.
Another example:

Program.cs

using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Console.WriteLine((int)Color.Yellow);
            Console.WriteLine((int)Color.Blue);
            Console.WriteLine((int)Color.Brown);
            Console.WriteLine((int)Color.Green);

Console.ReadLine();
}
}

enum Color
{
Yellow = 2,
Blue,
Brown = 9,
Green = Yellow
}
}

Output

2
3
9
2
Here we initialized Green to Yellow, and we did not get any error, so we see, more than one enum members can be initialized a same constant value.
Point to remember: More than one enum members can be initialized a same constant value.

Program.cs

using System;
namespace Enums
{
class Program
    {
static void Main(string[] args)
        {
            Color.Yellow = 3;
        }
    }

enum Color
{
Yellow = 2,
Blue,
Brown = 9,
Green = Yellow
}
}

Output

Compile time error: The left-hand side of an assignment must be a variable, property or indexer
In the above example, we tried to initialize the enum member out of the scope of defined enum, i.e., in another class, and got a compile time error. We must not forget that an enum acts as a constant, which cannot change its value.
Point to remember: An enum acts as a constant, so its value cannot be changed once initialized.

Readability

Program.cs

using System;

namespace Enums
{
internal enum Color
{
Yellow,
Blue,
Brown,
Green
}

internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(CheckColor(Color.Yellow));
Console.WriteLine(CheckColor(Color.Brown));
Console.WriteLine(CheckColor(Color.Green));
Console.ReadLine();
}

public static string CheckColor(Color color)
{
switch (color)
{
case Color.Yellow:
return Yellow”;
case Color.Blue:
return Blue”;
case Color.Brown:
return Brown”;
case Color.Green:
return Green”;
default:
return no color”;
}
}
}
}

Output

Yellow
Brown
Green
Here, in the above example, we have declared an enum Color containing various color members.There is a class named program that contains a static method named CheckColor, that has a switch statement checkingcolor on the basis of passed parameter to the method, i.e., Enum Color. In Main method, we try to access thatCheckColor method, passing various parameters. We see that the switch statement in CheckColor method can take any of the datatype passed and in return case statements use name of that type and not the plain intnumber to compare the result. We see that this made our program more readable. So enum plays an important role in making the program more readable and structured, easy to grasp.

Circular Dependency

Program.cs

using System;

namespace Enums
{
internal enum Color
{
Yellow=Blue,
Blue
}

internal class Program
{
private static void Main(string[] args)
{
}
}
}

Output

Compile time error: The evaluation of the constant value for 'Enums.Color.Yellow' involves a circular definition
Like constants, we also cannot have circular dependency in enums. We assigned valueBlue to Yellow, and Blue in turn is incremented by one as a next enum member, this results in a circular dependency of Blue to yellow, and resulted in error, C# is smart enough to catch these kind of tricks.

Diving Deep

Let’s take some complex scenarios:

Lab1

Program.cs

using System;

namespace Enums
{
enum Color
{

}

internal class Program
{
private static void Main(string[] args)
{
Color color = (Color) -1;
Console.ReadLine();
}
}
}

 
Note: Each and every code snippet in this article is tried and tested.

Output

Compile time error: 
To cast a negative value, you must enclose the value in parentheses
'Enums.Color' is a 'type' but is used like a 'variable'
In the above example, we are casting a negative value to enum, but the compiler says that while casting a negative value, we must keep that in parenthesis. It’s not strange, as C# knows that “-” is also a unary operator, that while using above code may create a confusion for compiler that we are using subtraction or typecasting a negative value. So always use parenthesis while typecasting negative values.

Lab2

Program.cs

using System;

namespace Enums
{
enum Color
{
value__
}

internal class Program
{
private static void Main(string[] args)
{

}
}
}

Output

Compile time error: The enumerator name 'value__' is reserved and cannot be used
We clearly see here that we have value__ as reserved member for the enumerator. C# compiler like this keyword has large number of reserved inbuilt keywords.
Image credit: www.vector.rs
It may keep this reserved keyword to keep track of the enum members internally but not sure.

Summary

Let’s recall all the points that we have to remember.
  1. An enum represents for a constant number, and an enum type is known as a distinct type having named constants.
  2. We can’t declare char as an underlying data type for enum objects because char stores Unicode characters, but enum objects data type can only be number.
  3. An enum can’t be derived from any other type except that of type bytesbyteshortushortintuint,long, or ulong.
  4. By default, enum is a sealed class and therefore sticks to all the rules that a sealed class follows, so no class can derive from enum, i.e., a sealed type.
  5. The enum type is implicitly derived from System.Enum and so we cannot explicitly derive it fromSystem.Enum.
  6. enum is also derived from three interfaces IComparableIFormattable and IConvertible.
  7. Numerous predefined conversion methods can be used to convert enum from one data type to another.
  8. More than one enum members can be initialized a same constant value.
  9. An enum acts as a constant, so its value cannot be changed once initialized.
  10. The enumerator name ‘value__‘ is reserved and cannot be used.

Conclusion

With this article, we completed almost all the scenarios related to enum. We did a lot of hands-on lab to clear our concepts. I hope my readers now know by heart about these basic concepts and will never forget them.
These may also help you in cracking C# interviews.
Keep coding and enjoy reading.
Also, do not forget to rate/comment/like my article if it helped you by any means, this helps me to get motivated and encourages me to write more and more.

Read more:

Other Series

My other series of articles:

For more informative articles visit my Blog.

For more technical articles you can reach out to CodeTeddy.

Read More

For more technical articles, you can reach out to A Practical Approach.

Diving into OOP (Day 5): All About C# Access Modifiers (Public/Private/Protected/Internal/Sealed/Constants/Static and Readonly Fields)


Introduction

Thanks to my readers for their tremendous support which has motivated me to continue this OOP series further.
We have already covered almost all the aspects of Inheritance and Polymorphism in C#. My article will highlight almost all the aspects/scenarios of access modifiers in C#. We’ll learn by doing hands on lab and not only by theory. We’ll cover my favourite topic Constants in a very different manner by categorizing the sections in the form of “Labs”. My effort in this article will be to cover each and every concept of the related topic, so that at the end of the article, we can confidently say that we know “All about access modifiers in C#”. Just dive into OOP.

Pre-requisites

I expect that readers of this article should have very basic knowledge of C#. The reader should only know the definition of access modifiers. Last but not the least, as always, I wish that my readers should enjoy reading this article.

Roadmap

Let’s recall our road map:

Access Modifiers

Let us take the definition from Wikipedia this time:

“Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.”

Like the definition says, we can control the accessibility of our class methods and members through access modifiers, let us understand this in detail by taking every access modifier one by one.

Public, Private, Protected at Class Level

Whenever we create a class, we always want to have the scope to decide who can access certain members of the class. In other words, we would sometimes need to restrict access to the class members. The one thumb rule is that members of a class can freely access each other. A method in one class can always access another method of the same class without any restrictions. When we talk about the default behavior, the same class is allowed complete access but no else is provided access to the members of the class. The default access modifier is private for class members.
Point to remember: The default access modifier is private for class members.
Let’s do some hands on lab. Just open your Visual Studio and add a console application in C# namedAccessModifiers. You’ll get a Program.cs class file by default. In the same file, add a new class named Modifiersand add the following code to it:
using System;

namespace AccessModifiers
{
class Modifiers
{
static void AAA()
{
Console.WriteLine(Modifiers AAA”);
}

public static void BBB()
{
Console.WriteLine(Modifiers BBB”);
AAA();
}
}

class Program
{
static void Main(string[] args)
{
Modifiers.BBB();
}
}
}

So, your Program.cs file becomes like shown in the above code snippet. We added a class Modifiers and two staticmethods AAA and BBB. Method BBB is marked as public. We call the method BBB from Main method.The method is called directly by the class name because it is marked static.
When we run the application, we get the output as follows:

Output

Modifiers BBB
Modifiers AAA
 
BBB is marked public and so anyone is allowed to call and run it. Method AAA is not marked with any access modifier which automatically makes it private, that is the default. The private modifier has no effect on members of the same class and so method BBB is allowed to call method AAA. Now this concept is called member access.
Modify the Program class and try to access AAA as:
class Program
    {
static void Main(string[] args)
        {
            Modifiers.AAA();
            Console.ReadKey();
        }
    }

Output

'AccessModifiers.Modifiers.AAA()' is inaccessible due to its protection level
So , since methodAAA is private, therefore no one else can have access to it except Modifiers class.
Note: Each and every code snippet written in this article is tried and tested.
 
Modifiers
Now mark the AAA method as protected, our class looks like:
class Modifiers
    {
protected static void AAA()
        {
            Console.WriteLine("Modifiers AAA");
        }

public static void BBB()
{
Console.WriteLine(Modifiers BBB”);
AAA();
}
}

Program

class Program
    {
static void Main(string[] args)
        {
            Modifiers.AAA();
            Console.ReadKey();
        }
    }

Output

'AccessModifiers.Modifiers.AAA()' is inaccessible due to its protection level
Again the same output. We cannot access the method AAA even after we introduced a new modifier namedprotected. But BBB can access AAA method because it lies in the same class.

Modifiers in Inheritance

Let’s add one more class and make a relation of base and derived class to our existing class and add one more method to our base class. So our class structure will look something like this:

Modifiers Base Class

class ModifiersBase
    {
static void AAA()
        {
            Console.WriteLine("ModifiersBase AAA");
        }
public static void BBB()
        {
            Console.WriteLine("ModifiersBase BBB");
        }
protected static void CCC()
        {
            Console.WriteLine("ModifiersBase CCC");
        }
    }

Modifiers Derive Class

class ModifiersDerived:ModifiersBase
    {
public static void XXX()
        {
            AAA();
            BBB();
            CCC();
        }
    }

Program Class

class Program
    {
static void Main(string[] args)
        {
            ModifiersDerived.XXX();
            Console.ReadKey();
        }
    }

Output

'AccessModifiers.ModifiersBase.AAA()' is inaccessible due to its protection level
Now in this case, we are dealing with derived class. Whenever we mark a method with the specifier, protected, we are actually telling C# that only derived classes can access that method and no one else can. Therefore in method XXX, we can call CCC because it is marked protected, but it cannot be called from anywhere else including Main function. The method AAA is made private and can be called only from the class ModifiersBase. If we remove AAA from method XXX, the compiler will give no error.
Therefore, now we are aware of three important concepts. Private means only the same class has access to the members, public means everybody has access and protected lies in between where only derived classes have access to the base class method.
All the methods for example reside in a class. The accessibility of that method is decided by the class in which it resides as well as the modifiers on the method. If we are allowed an access to a member, then we say that the member is accessible, else it is inaccessible.

Internal Modifier at Class Level

Let’s take one another scenario. Create a class library with a name “AccessModifiersLibrary” in your Visual Studio. Add a class named ClassA in that class library and mark the class as internal, the code will be as shown below:
AccessModifiersLibrary.ClassA:

namespace AccessModifiersLibrary
{
internal class ClassA
{
}
}

Now compile the class, and leave it. Its DLL will be generated in ~\AccessModifiersLibrary\bin\Debug folder.
Now in your console application, “AccessModifiers” i.e. created earlier. Add the reference ofAccessModifiersLibrary library by adding its compiled DLL as a reference to AccessModifiers.
In Program.cs of AccessModifiers console application, modify the Program class like shown below:

AccessModifiers.Program

using AccessModifiersLibrary;

namespace AccessModifiers
{
class Program
{
static void Main(string[] args)
{
ClassA classA;
}
}
}

And compile the code.

Output

Compile time error: 'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level
We encountered this error because the access specifier internal means that we can only access ClassA fromAccessModifiersLibrary.dll and not from any other file or code. Internal modifier means that access is limited to current program only. So try never to create a component and mark the class internal as no one would be able to use it.
And what if we remove the field internal from ClassA, will the code compile? i.e.,

AccessModifiersLibrary.ClassA

namespace AccessModifiersLibrary
{
class ClassA
    {
    }
}

AccessModifiers.Program

using AccessModifiersLibrary;

namespace AccessModifiers
{
class Program
{
static void Main(string[] args)
{
ClassA classA;
}
}
}

Output

Compile time error: 'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level
We again got the same error. We should not forget that by default if no modifier is specified, the class is internal. So our class ClassA is internal by default even if we do not mark it with any access modifier, so the compiler results remain the same.
Had the class ClassA been marked public, everything would have gone smooth without any error.
Point to remember: A class marked as internal can only be have its access limited to the current assembly only.

Namespaces with Modifiers

Let’s for fun, mark a namespace of AccessModifiers class library as public in Program class:

Program

public namespace AccessModifiers
{
class Program
    {
static void Main(string[] args)
        {

}
}
}

Compile the application.

Output

Compile time error: A namespace declaration cannot have modifiers or attributes
 
Point to remember: Namespaces as we see by default can have no accessibility specifiers at all. They are by defaultpublic and we cannot add any other access modifier including public again too.

Private Class

Let’s do one more experiment and mark the class Program as private, so our code becomes:
namespace AccessModifiers
{
private class Program
    {
static void Main(string[] args)
        {

}
}
}

Compile the code.

Output

Compile time error: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
Point to remember: A class can only be public or internal. It cannot be marked as protected or private. The default is internal for the class.
Access modifiers for the members of the class:
Now here is a big statement, that the members of a class can have all the above explained access modifiers, but default modifier is private.
Point to remember: Members of a class can be marked with all the access modifiers, and the default access modifier isprivate.
What if we want to mark a method with two access modifiers?
namespace AccessModifiers
{
public class Program
    {
static void Main(string[] args)
        {
        }

public private void Method1()
{

}
}
}

Compile the code.

Output

Compile time error: More than one protection modifier
Therefore, we can’t mark a member with more than one access modifier often. But there are such scenarios too, we’ll cover them in next sections. Already defined types like int and object have no accessibility restrictions. They can be used anywhere and everywhere.

Internal Class and Public Method

Create a class library with a class named ClassA marked internal and have a public method MethodClassA(), as:
namespace AccessModifiersLibrary
{
internal class ClassA
    {
public void MethodClassA(){}
    }
}
Add the reference of class library to our console application. Now in Program.cs of console application, try to access that method MethodClassA of ClassA.

Program

using AccessModifiersLibrary;

namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassA classA = new ClassA();
classA.MethodClassA();
}
}
}

Output

Compile time errors:
'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level
The type 'AccessModifiersLibrary.ClassA' has no constructors defined
'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level
'AccessModifiersLibrary.ClassA' does not contain a definition for 'MethodClassA' and
no extension method 'MethodClassA' accepting a first argument of type 'AccessModifiersLibrary.ClassA'
could be found (are you missing a using directive or an assembly reference?)
So many errors. The errors are self explanatory though. Even the method MethodClassA of ClassA is public, it could not be accessed in Program class due to protection level of ClassA, i.e. internal. The type enclosing the method MethodClassA is internal, so no matter if the method is marked public, we cannot access it in any other assembly.

Public Class and Private Method

Let’s make the class ClassA as public and method as private:
AccessModifiersLibrary.ClassA:

namespace AccessModifiersLibrary
{
public class ClassA
{
private void MethodClassA(){}
}
}

Program

using AccessModifiersLibrary;

namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassA classA = new ClassA();
classA.MethodClassA();
}
}
}

Output on compilation

'AccessModifiersLibrary.ClassA' does not contain a definition
for 'MethodClassA' and no extension method 'MethodClassA' accepting a first argument
of type 'AccessModifiersLibrary.ClassA' could be found (are you missing a using directive or an assembly reference?)
Now we marked our class Public, still can’t access the private method. So for accessing a member of the class, the access modifier of class as well as method is very important.
 
Note: Each and every code snippet written in this article is tried and tested.

Public Class and Internal Method

Make ClassA as public and MethodClassA as internal:
AccessModifiersLibrary.ClassA:

namespace AccessModifiersLibrary
{
public class ClassA
{
Internal void MethodClassA(){}
}
}

Program

using AccessModifiersLibrary;

namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassA classA = new ClassA();
classA.MethodClassA();
}
}
}

Output on compilation

'AccessModifiersLibrary.ClassA' does not contain a definition for 'MethodClassA' and no extension
method 'MethodClassA' accepting a first argument of type 'AccessModifiersLibrary.ClassA' could be
found (are you missing a using directive or an assembly reference?)
So an internal marked member means that no one from outside that DLL can access the member.

Protected Internal

In the class library, make three classes ClassAClassB and ClassC, and place the code somewhat like this:
namespace AccessModifiersLibrary
{
public class ClassA
    {
protected internal void MethodClassA()
        {

}
}

public class ClassB:ClassA
{
protected internal void MethodClassB()
{
MethodClassA();
}
}

public class ClassC
{
public void MethodClassC()
{
ClassA classA=new ClassA();
classA.MethodClassA();
}
}
}

And in Program class in our console application, call the MethodClassC of ClassC.

Program

using AccessModifiersLibrary;

namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassC classC=new ClassC();
classC.MethodClassC();
}
}
}

Compiler output

The code successfully compiles with no error.
 
Protected internal modifier indicates two things, that either the derived class or the class in the same file can have access to that method, therefore in the above mentioned scenario, the derived class ClassB and the class in the same file, i.e., ClassC can access that method of ClassA marked as protected internal.
 
Point to rememberProtected internal means that the derived class and the class within the same source code file can have access.

Protected Member

In our Program.cs in console application, place the following code:
namespace AccessModifiers
{
class AAA
    {
protected int a;
void MethodAAA(AAA aaa,BBB bbb)
        {
            aaa.a = 100;
            bbb.a = 200;
        }
    }
class BBB:AAA
     {
void MethodBBB(AAA aaa, BBB bbb)
         {
             aaa.a = 100;
             bbb.a = 200;
         }
     }
public class Program
    {
public static void Main(string[] args)
        {
        }
    }
}

Compiler Output

Cannot access protected member 'AccessModifiers.AAA.a' via a qualifier of type 'AccessModifiers.AAA';
the qualifier must be of type 'AccessModifiers.BBB' (or derived from it)
Class AAA is containing a protected member, i.e., a. But to the same class, no modifiers make sense. However as isprotected, in the derived class method MethodBBB, we cannot access it through AAA as aaa.a gives us an error. However bbb which looks like BBB does not give an error. To check this out, comment out the line aaa.a=100 inMethodBBB (). This means that we cannot access the protected members from an object of the base class, but from the objects of derived class only. This is in spite of the fact that is a member of AAA i.e. the base class. Even so, we still cannot access it. Also we cannot access from the method Main.

Accessibility Priority in Inheritance

Program

namespace AccessModifiers
{
class AAA
    {

}
public class BBB:AAA
{

}
public class Program
{
public static void Main(string[] args)
{
}
}
}

Compiler Output

Compile time error: Inconsistent accessibility: base class 'AccessModifiers.AAA' is less accessible than class 'AccessModifiers.BBB'
The error again gives us one more point to remember.
 
Point to remember: In between public and internalpublic always allows greater access to its members.
The class AAA is by default marked internal and BBB that derives from AAA is made public explicitly. We got an error as the derived class BBB has to have an access modifier which allows greater access than the base class access modifier. Here internal seems to be more restrictive than public.
But if we reverse the modifiers to both the classes i.e. ClassA marked as public and ClassB internal or default, we get rid of the error.
 
Point to remember: The base class always allows more accessibility than the derived class.
Another scenario:

Program

namespace AccessModifiers
{
class AAA
    {

}
public class BBB
{
public AAA MethodB()
{
AAA aaa= new AAA();
return aaa;
}
}
public class Program
{
public static void Main(string[] args)
{
}
}
}

Compiler output

Inconsistent accessibility: return type 'AccessModifiers.AAA' is less accessible than method 'AccessModifiers.BBB.MethodB()'
Here the accessibility of AAA is internal which is more restrictive than public. The accessibility of method MethodBis public which is more than that of the typeAAA. Now the error occurred because return values of a method must have greater accessibility than that of the method itself, which is not true in this case.
 
Point to remember: The return values of a method must have greater accessibility than that of the method itself.

Program

namespace AccessModifiers
{
class AAA
    {

}
public class BBB
{
public AAA aaa;
}
public class Program
{
public static void Main(string[] args)
{
}
}
}

Compiler Output

Inconsistent accessibility: field type 'AccessModifiers.AAA' is less accessible than field 'AccessModifiers.BBB.aaa'
Now rules are the same for everyone. The class AAA or data type aaa is internalaaa field is public which makes it more accessible than AAA which is internal. So we got the error.
Change the code to:
namespace AccessModifiers
{
class AAA
    {

}
public class BBB
{
AAA a;
}
public class Program
{
public static void Main(string[] args)
{
}
}
}

The output compilation results in no error.
We learnt a lot about these access modifiers like publicprivateprotectedinternalprotected internal. We also learnt about their priority of access and usage, let’s summarize their details in a tabular format for revision. Later, we’ll move to other types as well.
Tables taken from MSDN:
Declared accessibility Meaning
public Access is not restricted.
protected Access is limited to the containing class or types derived from the containing class.
internal Access is limited to the current assembly.
protected internal Access is limited to the current assembly or types derived from the containing class.
private Access is limited to the containing type.
“Only one access modifier is allowed for a member or type, except when you use the protected internalcombination.
Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.
Depending on the context in which a member declaration occurs, only certain declared accessibilities are permitted. If no access modifier is specified in a member declaration, a default accessibility is used.
Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.
Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.”
Members of Default member accessibility Allowed declared accessibility of the member
enum Public None
class Private public
protected
internal
private
protected internal
interface Public None
struct Private public
internal
private

Sealed Classes

Sealed” is a special class of access modifier in C#. If a class is marked as sealed, no other class can derive from thatsealed class. In other words, a class marked as sealed can’t act as a base class to any other class.

Program

namespace AccessModifiers
{
sealed class AAA
    {

}
class BBB:AAA
{

}
public class Program
{
public static void Main(string[] args)
{
}
}
}

Compiler Output

'AccessModifiers.BBB': cannot derive from sealed type 'AccessModifiers.AAA'
Hence proved.
 
Point to remember: A class marked sealed can’t act as a base class to any other class.
Access the members of sealed class.

Program

using System;

namespace AccessModifiers
{
sealed class AAA
{
public int x = 100;
public void MethodA()
{
Console.WriteLine(Method A in sealed class”);
}
}
public class Program
{
public static void Main(string[] args)
{
AAA aaa=new AAA();
Console.WriteLine(aaa.x);
aaa.MethodA();
Console.ReadKey();
}
}
}

Compiler Output

100
Method A in sealed class
So, as we discussed, the only difference between a sealed and a non sealed class is that the sealed class cannot be derived from. A sealed class can contain variables, methods, properties like a normal class do.
 
Point to remember: Since we cannot derive from sealed classes, the code from the sealed classes cannot be overridden.
 
Note: Each and every code snippet written in this article is tried and tested.

Constants

Lab1

Our Program class in the console application.

Program

public class Program
    {
private const int x = 100;
public static void Main(string[] args)
        {
            Console.WriteLine(x);
            Console.ReadKey();
        }
    }

Output

100
We see, a constant marked variable or a const variable behaves like a member variable in C#. We can provide it an initial value and can use it anywhere we want.
 
Point to remember: We need to initialize the const variable at the time we create it. We are not allowed to initialize it later in our code or program.

Lab2

using System;

namespace AccessModifiers
{
public class Program
{
private const int x = y + 100;
private const int y = z – 10;
private const int z = 300;

public static void Main(string[] args)
{
System.Console.WriteLine({0} {1} {2}”,x,y,z);
Console.ReadKey();
}
}
}

Can you guess the output? What? Is it a compiler error?

Output

390 290 300
Shocked? A constant field can no doubt depend upon another constant. C# is very smart to realize that to calculate the value of variable marked const, it first needs to know the value of y variable. y’s value depends upon anotherconst variable z, whose value is set to 300. Thus C# first evaluates to 300 then becomes 290 i.e. z -1 and finally x takes on the value of y i.e. 290 + 100 resulting in 390.

Lab3

Program

using System;

namespace AccessModifiers
{
public class Program
{
private const int x = y + 100;
private const int y = z – 10;
private const int z = x;

public static void Main(string[] args)
{
System.Console.WriteLine({0} {1} {2}”,x,y,z);
Console.ReadKey();
}
}
}

Output

The evaluation of the constant value for 'AccessModifiers.Program.x' involves a circular definition
We just assigned z=x from our previous code, and it resulted into error. The value of const x depends upon y, and yin turn depends upon value of z, but we see value depends upon as is assigned directly to z, it results in a circular dependency.
 
Point to remember: Like classes, const variables cannot be circular, i.e., they cannot depend on each other.

Lab4

const is a variable whose value once assigned cannot be modified, but its value is determined at compile time only.
using System;

namespace AccessModifiers
{
public class Program
{
public const ClassA classA=new ClassA();
public static void Main(string[] args)
{
}
}

public class ClassA
{

}
}

Output

Compile time error: 'AccessModifiers.Program.classA' is of type 'AccessModifiers.ClassA'.
A const field of a reference type other than string can only be initialized with null.
 
Point to remember: A const field of a reference type other than string can only be initialized with null.
If we assign the value to null in Program class:
using System;

namespace AccessModifiers
{
public class Program
{
public const ClassA classA=null;
public static void Main(string[] args)
{
}
}

public class ClassA
{

}
}

Then the error will vanish. The error disappears as we now initialize classA to an object which has a value that can be determined at compile time i.e., null. We can never change the value of classA, so it will always be null. Normally, we do not have consts as classA reference type as they have value only at runtime.
 
Point to remember: One can only initialize a const variable to a compile time value, i.e., a value available to the compiler while it is executing.
new() actually gets executed at runtime and therefore does not get value at compile time. So this results in an error.

Lab5

ClassA

public class ClassA
    {
public const int aaa = 10;
    }

Program

public class Program
    {
public static void Main(string[] args)
        {
            ClassA classA=new ClassA();
            Console.WriteLine(classA.aaa);
            Console.ReadKey();
        }
    }

Output

Compile time error: Member 'AccessModifiers.ClassA.aaa'
cannot be accessed with an instance reference; qualify it with a type name instead
 
Point to remember: A constant by default is static and we can’t use the instance reference, i.e., a name to reference a const. A const has to be static as no one will be allowed to make any changes to a const variable.
Just mark the const as static.
using System;

namespace AccessModifiers
{
public class ClassA
{
public static const int aaa = 10;
}

public class Program
{
public static void Main(string[] args)
{
ClassA classA=new ClassA();
Console.WriteLine(classA.aaa);
Console.ReadKey();
}
}
}

Output

Compile time error: The constant 'AccessModifiers.ClassA.aaa' cannot be marked static
C# tells us frankly that a field i.e. already static by default cannot be marked as static.
Point to remember: A const variable cannot be marked as static.

Lab6

using System;

namespace AccessModifiers
{
public class ClassA
{
public const int xxx = 10;
}

public class ClassB:ClassA
{
public const int xxx = 100;
}

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(ClassA.xxx);
Console.WriteLine(ClassB.xxx);
Console.ReadKey();
}
}
}

Output

10
100
Compiler Warning: 'AccessModifiers.ClassB.xxx' hides inherited
member 'AccessModifiers.ClassA.xxx'. Use the new keyword if hiding was intended.
We can always create a const with the same name in the derived class as another const in the base class. The constvariable of class ClassB xxx will hide the const xxx in class ClassA for the class ClassB only.

Static Fields

Point to remember: A variable in C# can never have an uninitialized value.
Let’s discuss this in detail.

Lab1

Program

using System;

namespace AccessModifiers
{
public class Program
{
private static int x;
private static Boolean y;
public static void Main(string[] args)
{
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadKey();
}
}
}

Output

0
False
 
Point to rememberStatic variables are always initialized when the class is loaded first. An int is given a default value of zero and a bool is given a default to False.

Lab2

Program

using System;

namespace AccessModifiers
{
public class Program
{
private int x;
private Boolean y;
public static void Main(string[] args)
{
Program program=new Program();
Console.WriteLine(program.x);
Console.WriteLine(program.y);
Console.ReadKey();
}
}
}

Output

0
False
 
Point to remember: An instance variable is always initialized at the time of creation of its instance.
An instance variable is always initialized at the time of creation of its instance. The keyword new will create an instance of the class Program. It will allocate memory for each of the non static, i.e. instance variables and then initialize each of them to their default values as well.

Lab3

Program

using System;

namespace AccessModifiers
{
public class Program
{
private static int x = y + 10;
private static int y = x + 5;
public static void Main(string[] args)
{
Console.WriteLine(Program.x);
Console.WriteLine(Program.y);
Console.ReadKey();
}
}
}

Output

10
15
Output is self explanatory. C# always initializes static variables to their initial value after creating them. Variables xand are therefore given a default of zero value. C# now realizes that these variables declared need to be assigned some values. C# does not read all the lines at once but only one at a time. It will now read the first line and as the variable y has a value of 0, so will get a value of 10. Then at the next line, is the value of x + 5. The variable xhas a value of 10 and so now becomes 15. As C# does not see both lines at the same time, it does not notice the circularity of the above definition.

Lab4

Program

using System;

namespace AccessModifiers
{
public class Program
{
int x = y + 10;
int y = x + 5;
public static void Main(string[] args)
{

}
}
}

Output

Compile time error:

A field initializer cannot reference the non-static field, method, or property ‘AccessModifiers.Program.y’
A field initializer cannot reference the non-static field, method, or property ‘AccessModifiers.Program.x’

The lab we did in Lab3 does not work for instance variables as the rules of an instance variable are quite different than that of static variables. The initializer of an instance variable has to be determined at the time of creation of the instance. The variable y does not have a value at this point in time. It can’t refer to variables of the same object at the time of creation. So we can refer to no instance members to initialize an instance member.

Readonly Fields

Readonly fields are one of the most interesting topics of OOP in C#.

Lab1

Program

using System;

namespace AccessModifiers
{
public class Program
{
public static readonly int x = 100;

public static void Main(string[] args)
{
Console.WriteLine(x);
Console.ReadKey();
}
}
}

Output

100
Wow, we get no error, but remember not to use a non static variable inside a static method, else we’ll get an error.

Lab2

Program

using System;

namespace AccessModifiers
{
public class Program
{
public static readonly int x = 100;

public static void Main(string[] args)
{
x = 200;
Console.WriteLine(x);
Console.ReadKey();
}
}
}

Output

Compile time error: A static readonly field cannot be assigned to
(except in a static constructor or a variable initializer).
We cannot change the value of a readonly field except in a constructor.
Point to remember: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer).

Lab3

Program

using System;

namespace AccessModifiers
{
public class Program
{
public static readonly int x;

public static void Main(string[] args)
{
}
}
}

Here we find one difference between const and readonly, unlike constreadonly fields need not have to be initialized at the time of creation.

Lab4

Program

using System;

namespace AccessModifiers
{
public class Program
{
public static readonly int x;

static Program()
{
x = 100;
Console.WriteLine(Inside Constructor”);
}

public static void Main(string[] args)
{
Console.WriteLine(x);
Console.ReadKey();
}
}
}

Output

Inside Constructor
100
One more major difference between const and readonly is seen here. A static readonly variable can be initialized in the constructor as well, like we have seen in the above mentioned example.

Lab5

Program

using System;

namespace AccessModifiers
{
public class ClassA
{

}
public class Program
{

public readonly ClassA classA=new ClassA();
public static void Main(string[] args)
{
}
}
}

We have already seen this example in const section. The same code gave an error with const does not give an error with readonly fields. So we can say that readonly is a more generic const and it makes our programs more readable as we refer to a name and not a number. Is 10 more intuitive or priceofcookie easier to understand? The compiler would for efficiency convert all consts and readonly fields to the actual values.

Lab6

Program

using System;

namespace AccessModifiers
{
public class ClassA
{
public int readonly x= 100;
}
public class Program
{
public static void Main(string[] args)
{
}
}
}

Output

Compile time error:

Member modifier ‘readonly’ must precede the member type and name
Invalid token ‘=’ in class, struct, or interface member declaration

Wherever we need to place multiple modifiers, remind yourself that there are rules that decide the order of access modifiers, which comes first. Now here the readonly modifier precedes the data type int, we already discussed at the very start of the article. This is just a rule that must always be remembered.

Lab7

Program

using System;

namespace AccessModifiers
{
public class ClassA
{
public readonly int x= 100;

void Method1(ref int y)
{

}

void Method2()
{
Method1(ref x);
}
}
public class Program
{

public static void Main(string[] args)
{
}
}
}

Output

Compile time error:

A readonly field cannot be passed ref or out (except in a constructor)
A readonly field can’t be changed by anyone except a constructor.
The method Method1 expects a ref parameter which if we have forgotten allows you
to change the value of the original. Therefore C# does not permit a readonly
as a parameter to a method that accepts a ref or an out parameters.

Summary

Let’s recall all the points that we have to remember:
  1. The default access modifier is private for class members.
  2. A class marked as internal can have its access limited to the current assembly only.
  3. Namespaces as we see by default can have no accessibility specifiers at all. They are by default public and we cannot add any other access modifier including public again too.
  4. A class can only be public or internal. It cannot be marked as protected or private. The default isinternal for the class.
  5. Members of a class can be marked with all the access modifiers, and the default access modifier is private.
  6. Protected internal means that the derived class and the class within the same source code file can have access.
  7. Between public and internalpublic always allows greater access to its members.
  8. Base class always allows more accessibility than the derived class.
  9. The return values of a method must have greater accessibility than that of the method itself.
  10. A class marked sealed can’t act as a base class to any other class.
  11. Since we cannot derive from sealed classes, the code from the sealed classes cannot be overridden.
  12. We need to initialize the const variable at the time we create it. We are not allowed to initialize it later in our code or program.
  13. Like classes, const variables cannot be circular, i.e., they cannot depend on each other.
  14. const field of a reference type other than string can only be initialized with null.
  15. One can only initialize a const variable to a compile time value, i.e., a value available to the compiler while it is executing.
  16. A constant by default is static and we can’t use the instance reference, i.e., a name to reference a const. Aconst has to be static as no one will be allowed to make any changes to a const variable.
  17. const variable cannot be marked as static.
  18. A variable in C# can never have an uninitialized value.
  19. Static variables are always initialized when the class is loaded first. An int is given a default value of zero and a bool is given a default of False.
  20. An instance variable is always initialized at the time of creation of its instance.
  21. static readonly field cannot be assigned to (except in a static constructor or a variable initializer).

Conclusion

With this article, we completed almost all the scenarios of access modifiers. We did a lot of hands-on lab to clear our concepts. I hope my readers now know by heart about these basic concepts and will never forget them. In my upcoming article, i.e., the last article of this series, we’ll be discussing about Properties and Indexers in C#.
Keep coding and enjoy reading. 
Also do not forget to rate/comment/like my article if it helped you by any means, this helps me get motivated and encourages me to write more and more.

Read more:

Other Series

My other series of articles:

For more informative articles visit my Blog.

For more technical articles you can reach out to CodeTeddy.

Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Classes in C#)


1. Introduction

We learnt a lot about polymorphism and inheritance. In this article of the series “Diving in OOP”, we’ll discuss about the most hot and exciting topic of OOP in C#, i.e., Abstract Classes. The concept of Abstract classes is the same for any other language, but in C# we deal with it in a bit different way. Abstract classes play a different and very interesting role in polymorphism and inheritance. We’ll cover all the aspects of abstract classes with our hands-on lab and theory as an explanation to what output we get. We’ll also list down points to remember at the end of the article.

Pre-requisites

Wonder, we are dealing with the fourth part of our learning objective. Now my only expectation with my readers is to enjoy the series.

2. Roadmap

Let’s recall our road map:

3. Abstract Classes

Let’s get the definition from MSDN:

“The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.
Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method.”

4. Abstract Classes in Action

Add a console application named “InheritanceAndPolymorphism” in your Visual Studio. You’ll get a class namedProgram.cs, just add one more class named ClassA.cs, note that the ClassA should be marked abstract, and the following code to ClassA.cs and Program.cs:
using System;

namespace InheritanceAndPolymorphism
{
public abstract class ClassA
{

}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassA classA = new ClassA();
Console.ReadKey();
}
}
}

Compile the code.

Output

Compile time error: Cannot create an instance of the abstract class or interface 'InheritanceAndPolymorphism.ClassA'
 
Point to remember: We cannot create an object of abstract class using new keyword.
Now we go into understanding the concept. No power can stop abstract keyword to be written before a class. It acts as a modifier to the class. We cannot create an object of abstract class using new keyword. Seems that the class is useless for us as we cannot use it for other practical purposes as we used to do.

5. Non Abstract Method Definition in Abstract Class

Let’s add some code to our abstract class:
    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassA classA = new ClassA();
Console.ReadKey();
}
}

We again see the error that we encountered earlier. Again, it reminds that we cannot use new if we have already used an abstract modifier.

6. Abstract Class Acting as a Base Class

Let’s add one more class now:
    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA
/// </summary>
public class ClassB:ClassA
{

}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

We get no error? A class can be derived from abstract class. Creating an object of ClassB does not gives us any error.
 
Point to remember: A class can be derived from an abstract class.
 
Point to remember: A class derived from an abstract class can create an object.

7. Non Abstract Method Declaration in Abstract Class

Another scenario:
    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}

public void YYY();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{

}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

We just declared a method named YYY() in our abstract class ClassA.
Compile the code, we get:

Output

Compile time error: 'InheritanceAndPolymorphism.ClassA.YYY()' 
must declare a body because it is not marked abstract, extern, or partial
 
InheritanceAndPolymorphism is the namespace I used for my console application so you can ignore that, no need to confuse with the logic.
In the above code, we just added a method declaration in the abstract class. An abstract method indicates that the actual definition or code of the method is created somewhere else. The method prototype declared in abstractclass must also be declared abstract as per the rules of C#.

8. Abstract Method Declaration in Abstract Class

Just make the method YYY() as abstract in ClassA:
    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}

abstract public void YYY();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{

}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

Output

Compiler error: 'InheritanceAndPolymorphism.ClassB' does not implement 
inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY()'
 
Point to remember: If we declare any method as abstract in our abstract class, then it’s the responsibility of the derived class to provide the body of that abstract method, unless a body is provided for that abstract method, we cannot create an object of that derived class.
In the above mentioned scenario, we declared method YYY() as abstract in ClassA. Since ClassB derives fromClassA, now it becomes the responsibility of ClassB to provide the body of that abstract method, else we cannot create an object of ClassB.

9. Abstract Method Implementation in Derived Class

Now provide a body of method YYY() in ClassB. Let’s see what happens:
    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}

abstract public void YYY();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{
public void YYY()
{

}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

Everything seems fine now, but no? Compile the code, what we get:

Output

Two compile time errors this time:
Compile time error: 'InheritanceAndPolymorphism.ClassB' does not implement 
inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY()'

Compile time warning: ‘InheritanceAndPolymorphism.ClassB.YYY()’ hides
inherited member ‘InheritanceAndPolymorphism.ClassA.YYY()’.

To make the current member override that implementation, add the override keyword. Otherwise add the newkeyword.
We have been continuously trying to compile our code, but no success till now. The compiler error indicates clearly that both of our base and derived class contains the same method named YYY().
If both our derived class and base class contain the method with the same name, always an error occurs. The only way to overcome this error is derived class explicitly add the modifier override to its method signature. We have already discussed such scenarios in our previous parts of the articles of Diving in OOP series.
Let’s add the override keyword before derived class method YYY().
/// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}

abstract public void YYY();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{
public override void YYY()
{

}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

We get no warning or error now?

10. Abstract Method Implementation in Derived Class with Different Return Type

Let’s just change the return type of the method YYY() in derived class:
  /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}

abstract public void YYY();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{
public override int YYY()
{

}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

We changed return type of method YYY from void to int in derived class. Compile the code.

Output

Compile time error: 'InheritanceAndPolymorphism.ClassB.YYY()': return type must be 'void' 
to match overridden member 'InheritanceAndPolymorphism.ClassA.YYY()'
Therefore one more constraint.
 
Point to remember: When we override an abstract method from a derived class, we cannot change the parameters passed to it or the return type irrespective of the number of methods declared as abstract in abstractclass.
Let’s see the implementation of the second line mentioned in “point to remember”,
    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}

abstract public void YYY();
abstract public void YYY1();
abstract public void YYY2();
abstract public void YYY3();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{
public override int YYY()
{

}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

Compiler error:
'InheritanceAndPolymorphism.ClassB' does not implement 
inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY3()'

‘InheritanceAndPolymorphism.ClassB’ does not implement inherited
abstract member ‘InheritanceAndPolymorphism.ClassA.YYY2()’

‘InheritanceAndPolymorphism.ClassB’ does not implement inherited
abstract member ‘InheritanceAndPolymorphism.ClassA.YYY1()’

If we implement these three methods in derived class, we’ll get no error.
Point to remember: An abstract class means that the class is incomplete and cannot be directly used. Anabstract class can only be used as a base class for other classes to derive from.

11. Variable Initialization in Abstract Class

Therefore as seen earlier, we get an error if we use a new keyword on an abstract class. If we do not initialize a variable in an abstract class like we used a, it will automatically have a default value of 0 which is what the compiler kept warning us about. We can initialize int variable a of ClassA to any value we wish. The variables in abstractclass act similar to that in any other normal class.

12. Power of Abstract Class

Whenever a class remains incomplete, i.e., we do not have the code for some methods, we mark those methodsabstract and the class is marked abstract as well. And so, we can compile our class without any error or blocker. Any other class can then derive from our abstract class but they have to implement the abstract, i.e., our incomplete methods from abstract class.
Abstract therefore enables us to write code for a part of the class and allows the others (derived classes) to complete the rest of the code.

13. Abstract Method in Non Abstract Class

Let’s take another code block:
    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public class ClassA
    {
        public int a;
        public void XXX()
        {

}

abstract public void YYY();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{
public override void YYY()
{

}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

Compile the code.

Output

Compiler error: 'InheritanceAndPolymorphism.ClassA.YYY()' is abstract 
but it is contained in non-abstract class 'InheritanceAndPolymorphism.ClassA'
We just removed abstract keyword from class ClassA. The error clearly conveys a message that if a single method is marked abstract in a class, then the class will have to be abstract as well.
 
Point to remember: If a class has even a single abstract method, then the class has to be declared abstract as well.
 
Point to remember: An abstract method also cannot use the modifiers such as static or virtual.
We can only have the abstract method in an abstract class. Any class that derives from abstract class has to give implementation to its abstract method. By default, the modifier new gets added to the derived class method, that makes it a new/different method.

14. Abstract Base Method

    /// <summary>
    /// Abstract class ClassA
    /// </summary>
    public abstract class ClassA
    {
        public int a;
        public void XXX()
        {

}

abstract public void YYY();
}

/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{
public override void YYY()
{
base.YYY();
}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassB classB = new ClassB();
Console.ReadKey();
}
}

Output

Compile time error : Cannot call an abstract base member: 
'InheritanceAndPolymorphism.ClassA.YYY()'
We cannot call the method YYY() from the base class ClassA as it does not carry any implementation/code along with it and has also been declared abstract. Common sense prevails? and C# off course does not allow us to call a method that does not contain code.

15. Abstract Class Acting as Derived as Well as Base Class

Let’s modify our code a bit, and prepare our class structure something as follows:
    /// <summary>
    /// Base class ClassA
    /// </summary>
    public class ClassA
    {
        public virtual void XXX()
        {
            Console.WriteLine("ClassA XXX");
        }
    }

/// <summary>
/// Derived abstract class.
/// Class derived from base class ClassA.
/// </summary>
public abstract class ClassB:ClassA
{
public new abstract void XXX();
}

public class ClassC:ClassB
{
public override void XXX()
{
System.Console.WriteLine(ClassC XXX”);
}
}

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassA classA = new ClassC();
ClassB classB = new ClassC();
classA.XXX(); classB.XXX();
}
}

Compile the code, and run.

Output

ClassA XXX
ClassC XXX
We created a base class named ClassA that is not abstract and added a virtual method XXX to it. Since the method is non abstract but marked virtual so it has to be overridden in its deriving class. We added one more class named ClassB and marked that class abstract, note that this class is derived from ClassA. So this class has a choice to override the method marked as virtual in base class. But we’ll do something different and tricky,
We marked XXX method in this derived class as new abstract, and did not give anybody to this method. Now what? We will add one more class ClassC, that will derive from ClassBClassC has no choice but to override the methodXXX. Therefore we override the method XXX in ClassC.
In main method, we created two objects ClassA classA = new ClassC(); and ClassB classB = new ClassC();
First object looks like that of ClassC but refers to ClassA and second one again seems to be like ClassC but refers to ClassB.
In case of classA.XXX() will definitely first look into the class ClassA. Here, it finds the method XXX marked as virtual. These kind of scenarios we have already taken n number of times in our earlier articles where we discussed about run time polymorphism . C# will then crawl over to class ClassB. Here it gets shocked that the method XXX()is abstract, i.e., there is no code or implementation for method XXX() and also that it is a method marked as new, thus severing all links with the base class. And so flow halts and all and the method XXX() from ClassA gets executed.
In the case of b.XXX()(), since the method is new, the links to the base class gets broken, we are left with no choice but to invoke the method from ClassC as it says override.
We cannot replace the modifier new with the keyword override for the method XXX() in abstract class ClassB.
Let’s replace the override modifier in ClassC with “new” like:
 public class ClassC:ClassB
     {
        public new void XXX()
        {
            System.Console.WriteLine("ClassC XXX");
        }
     }

Output

Compile time error: 'InheritanceAndPolymorphism.ClassC' does not implement 
inherited abstract member 'InheritanceAndPolymorphism.ClassB.XXX()'
The error indicates that as there is no code for the method XXX. Remember the XXX() of class ClassA has nothing to do at all with that of ClassB and ClassC.
Also there is one more point to remember.
 
Point to remember: Virtual methods run slower that non virtual methods.

16. Can Abstract Class be Sealed?

Let’s take this final question into our consideration. Let’s test this too with an example.
    /// <summary>
    /// sealed abstract class ClassA
    /// </summary>
    public sealed abstract class ClassA
    {
        public abstract void XXX()
        {
            Console.WriteLine("ClassA XXX");
        }
    }

/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
}
}

Compile the code.

Output

Compile time error: 'InheritanceAndPolymorphism.ClassA': 
an abstract class cannot be sealed or static
And so we get two points to remember.
 
Point to rememberAbstract class cannot be sealed class.
 
Point to rememberAbstract class cannot be a static class.

17. Points to Remember

Let’s sum up all the points to remember:
  1. We cannot create an object of abstract class using new keyword.
  2. A class can be derived from an abstract class.
  3. Class derived from an abstract class can create an object.
  4. If we declare any method as abstract in our abstract class, then it’s the responsibility of the derived class to provide the body of that abstract method, unless a body is provided for that abstract method, we cannot create an object of that derived class.
  5. When we override an abstract method from a derived class, we cannot change the parameters passed to it or the return type irrespective of the number of methods declared as abstract in abstract class.
  6. An abstract class means that the class is incomplete and cannot be directly used. An abstract class can only be used as a base class for other classes to derive from.
  7. If a class has even a single abstract method, then the class has to be declared abstract as well.
  8. An abstract method also cannot use the modifiers such as static or virtual.
  9. Virtual methods run slower that non virtual methods.
  10. Abstract class cannot be sealed class.
  11. Abstract class cannot be a static class.

18. Conclusion

With this article, we complete our understanding of inheritance and polymorphism. We have covered almost all the aspects of Polymorphism and Inheritance. Abstract classes are one of my favorites so I just wanted to take them separately. I hope my readers enjoyed this article too and learnt about abstract classes in C#.
In my upcoming articles of the series, we’ll be discussing about other OOP features in the C# way with full hands-on lab and lot of discussion.
Keep coding and enjoy reading. 
Also do not forget to rate/comment/like my article if it helped you by any means. This helps me to get motivated and encourages me to write more and more.

Read more:

Other Series

My other series of articles:

For more informative articles visit my Blog.

For more technical articles you can reach out to CodeTeddy.