Skip to main content

Classes in C# Explained

C# Class Explained

A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99.

For instance, if you want to work with Guest’s data as in our previous DataDriven Web application.
The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data.
All of these operations can be represented as a class in C# as shown below.
using System;

namespace CsharpnaijaClassTutorial
{
    public class Guest
    {
        public int Id { get; set; }
        public string GuestName { get; set; }
        public string Address { get; set; }
        public string WhomToSee { get; set; }
        public DateTime DateVisited { get; set; }
        public DateTime TimeVisited { get; set; }

       public void AddGuest(Guest guest)
        {
            //Add guest property to data storage
        }
        public List<Guest> GetGuests()
        {
            return new List<Guest>();
        }
    }
}

The snippets above shows a class with some properties
In this post, we will look at how we can work with classes and their types in C#.

Type of Classes

We have basically the following types of Classes
1.     Abstract Class
2.     Concrete Class and
3.     Sealed Class

Abstract Classes

These are classes marked by the keyword abstract in their class definition, they are typically used to define a base class in the hierarchy. The special aspect of them, is that, you can't create an instance of them - if you try, you will get a compile error. Instead, you have to subclass them as shown below.

    public abstract class Blog
    {
        public int id { get; set; }
        public string Title { get; set; }

    }


As you can see from image above, we are trying to create an object of Blog class that is defined as abstract by using abstract keyword, we received a red underline on new Blog() signifying a compile error.

Now let’s make use of the Blog by defining another class called Csharpnaija that inherits the Blog class.

    public abstract class Blog
    {
        public int id { get; set; }
        public string Title { get; set; }

    }
    public class Csharpnaija:Blog
    {
        public string Author { get; set; }
    }


From the above code, we can now see Csharpnaija inherits Blog as the base class, but added another property called Author. Now Csharpnaija has three properties which include Id, Title and Author and we can now create an instance of the Csharpnaija.


The Csharpnaija has now becomes a concrete class of Blog class
Features Abstract Classes
1.     Use as base class
2.     Cannot be instantiated
3.     Enhance polymorphism
4.     Encourage Inheritance

Concrete Classes

A concrete class in C# is a type of subclass, which implements all the abstract method of its super abstract class which it extends to. It also has implementations of all methods of interfaces it implements. This case, our Csharpnaija class is a concrete class that inherited all properties of Blog class.

Concrete classes have the following features;

1.     can create instance as well as can be inherited
2.     All functionality completed
3.     In application class hierarchy, concrete class need not be in the first level
4.     No method should be abstract

Sealed Classes

These are classes used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, it cannot be inherited. 
The sealed modifier is used to declare a class as sealed in C#, it is the same as NotInheritable in Visual Basic .NET. If a class is derived from a sealed class, compiler throws an error. Structs are sealed. You cannot derive a class from a struct. 

    public sealed class SealedBlog
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
    }


We can create an instance or object of a sealed class, in the case of this post, an object of SealedBlog is instantiated and initialized as shown below.
The output is as shown below

Features of Sealed Classes
1.     Cannot be inherited
2.     take away the inheritance feature

Comments

  1. Are you looking for online tuition in Chandigarh for your child so that your child can complete his/her studies sitting at home without any hassle, then you should take online tutoring from Ziyyara because all the kids studying near Ziyyara always get good marks. Don't delay register today.
    Call Us For Free Demo:- +91 9654271931.
    Get Free Demo:- https://ziyyara.in/ad-contact
    Visit Us:- Online Tuition In Chandigarh

    ReplyDelete

Post a Comment

Popular posts from this blog

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

Most Popular Programming Languages in 2020

Most Popular Programming Languages in 2020 In this blog post, you will learn about the most popular programming languages in 2020 for creating the best web applications. Check its pros and cons. Analyzed by technostacks Not very long ago, just a few people were considered to be computer programmers, and the general public viewed them with awe. In this digital age that we are now living in, however, a large number of IT jobs need a solid grasp of one or more programming languages. Whether one wants to develop a mobile app or get a certification for having programming knowledge, or even to learn new skills, one needs to opt for the right programming language. Below mentioned eight most popular programming languages which are in demand for software development and web applications. This is the most used programming languages in 2019 and will be in 2020. For each, there is little information about the language, benefits and its complexity, as well as about its usage. One must...

HashTable in C# with Example

  HashTable in C# with Example Hashtable  is used to store a collection of key/value pairs of different  data types  and are organized based on the hash code of the key.   Generally, the hashtable object will contain buckets to store elements of the collection. The bucket here, is a virtual subgroup of elements within the hashtable and each bucket is associated with a hash code, which is generated based on the key of an element.   In C#, hashtable is same as a  dictionary  object but the only difference is that the  dictionary  object is used to store a key-value pair of same  data type  elements.   When compared with  dictionary  object, the hashtable will provide a lower performance because the hashtable elements are of object type so the boxing and unboxing process will occur when we are storing or retrieving values from the hashtable.   C# HashTable Declaration Hashtable is a non-generic type...