Abstract Property In C# Language

Introduction

Abstract property create on abstract member, which is use in the abstract class. You are define abstract property in a class , when inherit the abstract class in other non-abstract class. 

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Abstract_property_in_c_sharp

{

    public abstract class Details    //create abstract class

    {

        public abstract string Name  //create abstract property Name

        {

            get;

            set;

        }

        public abstract int Id       //create abstract property Id

        {

            get;

            set;

        }

    }

    class User : Details             //inherit abstract class

    {

 

        private string city = " ";

        private string name = " ";

        private int id = 0;

       

        public string City      // Declare a City property of type string:

        {

            get

            {

                return city;

            }

            set

            {

                city = value;

            }

        }

       

        public override string Name   // Declare abstract Name property:

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

               

        public override int Id       // Declare abstract Id property:

        {

            get

            {

                return id;

            }

            set

            {

                id = value;

            }

        }

        public override string ToString()

        {

            return "Id= "  + Id + ",   Name= " + Name + ",   City= " + City ;

        }

 

        public static void Main()

        {

 

            User us1 = new User();    // Create a new User object:

            us1.City = "Noida";       // Setting City, Name and the Id of the user

            us1.Name = "Arvind";

            us1.Id = 100;

            Console.WriteLine("Student Info:- {0}", us1);

            us1.Id += 1;                //let us increase Id

            Console.WriteLine("Student Info:- {0}", us1);

         

        }

    }

}


Output:


output.jpg



Ebook Download
View all
Learn
View all