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

ASP.NET MVC Views

Views in ASP.NET MVC Application explained Find a related article By  Steve Smith  and  Luke Latham from Microsoft Corporation here In the Model-View-Controller (MVC) pattern, the  view  handles the application's data presentation and user interaction. A view is an HTML template with embedded  Razor markup . Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET MVC, views are  .cshtml  files that use the  C# programming language  in Razor markup. Usually, view files are grouped into folders named for each of the application's  controllers . The folders are stored in a  Views  folder at the root of the application as shown: The  Home  controller is represented by a  Home  folder inside the  Views  folder.  The  Home  folder contains the views for the  About ,  Contact , and  Index...

ASP.NET MVC Routing

ASP.NET MVC Routing ASP.NET MVC routing is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match. Routing pattern is as follows A URL is requested from a browser, the URL is parsed (that is, break into controller and action), the parsed URL is compared to registered route pattern in the framework’s route table, if a route is found, its process and send response to the browser with the required response, otherwise, the HTTP 404 error is send to the browser. Route Properties ASP.NET MVC routes are res...