Encapsulation In C#

Encapsulation In C#

  • Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
  • Encapsulation in C# is a mechanism of wrapping the data (variables) and code acting on the data (methods or properties) together as a single unit.
  • In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods or properties of their current class. Therefore, it is also known as data hiding.
  • In a different way, encapsulation is a protective shield that prevents the data from being accessed by the code outside this shield.
  • Encapsulation is the procedure of encapsulating data and functions into a single unit.

To achieve encapsulation in C#

  1. Declare the variables of a class as private.
  2. Provide public setter and getter methods or properties to modify and view the variables values.

Benefits Of Encapsulation

  • The fields of a class can be made read-only or write-only.
  • A class can have total control over what is stored in its fields.

Why Do We Need Encapsulation?

The need of encapsulation is to protect or prevent the code (data) from accidental corruption due to the silly little errors that we are all prone to make.

Practical Example Of Encapuslation

Source Code For Encapsulation

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

namespace Encapsulation_Demo
{

    // PROPERTIES IN C#

    class Person
    {
        private string Name;
        private int Age;

        public void setName(string Name) // setter method
        {
            if(string.IsNullOrEmpty(Name) == true)
            {
                Console.WriteLine("Name is Must..");
            }
            else
            {
                this.Name = Name;
            }
            
        }

        //public void getName() // getter method
        //{
        //    if (string.IsNullOrEmpty(this.Name) == true)
        //    {
               
        //    }
        //    else
        //    {
        //        Console.WriteLine("Your name is: " + this.Name);
        //    }
            

        //}

        public void setAge(int Age) // setter method
        {
            if(Age > 0)
            {
                this.Age = Age;
            }
            else
            {
                Console.WriteLine("Age cannot be zero or negative");
            }
            
        }

        public void getAge() // getter method
        {
            if (Age > 0)
            {
                Console.WriteLine("Your age is: " + this.Age);
            }
            else
            {
                
            }
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1.setName("Adil");
            p1.setAge(-27);

            //p1.getName();
            p1.getAge();
            Console.ReadLine();


        }
    }
}

Click Below Link to Download Notes Of This Blog

https://www.mediafire.com/file/s1c7131bhx79oeb/ENCAPSULATION+IN+CSHARP.pptx/file

Click Below Link to Download Source Code Of This Blog

https://www.mediafire.com/file/sfpff1whccai3yf/Encapsulation_Demo.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *