Wednesday, May 17, 2017

சி-ஷார்ப்பில் ப்ராபர்டிகள்



நாம் பொதுவாக நிரல் எழுதும் பொழுது கிளாஸின் fields என்பதை private ஆகவும் மெதட்களை public ஆகவும் அறிவிப்போம்.கிளாஸிற்கு ஒரு டேட்டா மெம்பரை(fields) அணுக வேண்டுமானால் நேரடியாக முடியாது. மெத்தட்கள்  மூலம் தான் அணுக முடியும். இதுவே data hiding மற்றும் Encapsulation எனப்படுகின்றது.இதற்கு மாற்றாக ப்ராப்பர்டிகளை public ஆக அறிவித்து அதை fields என்பதை உபயோகிப்பது போல் உபயோகிக்கலாம்.
இதில் get மற்றும் set accessors மூலம் டேட்டா மெம்பர்களின் மதிப்புகளை பெறவோ அல்லது அதில் இருத்தலோ முடியும்.இப்பொழுது ப்ரொபெர்டிகளை fields –ஆனதை உபயோகிப்பது போல் உபயோகிக்கலாம். ஆனாலும் டேட்டா ஆனது encapsulate செய்யப் பட்டு தான் இருக்கும்.

சான்று நிரல்;

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

namespace propertyImplementation
{
    class student
    {
        private int _id;
        private string _name;
        public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student s1 = new student();
            s1.Id = 101;
            s1.Name = "karthikeyan";
            Console.WriteLine("Id= {0} && name= {1}", s1.Id, s1.Name);
            Console.ReadLine();
        }
    }
}

வெளியீடு:

 

மேலே உள்ள நிரலில் Id மற்றூம் Name எங்கின்ற இரண்டு ப்ரொப்பர்டிகளில் get மற்றும் set accessors மூலம் முறையே _id,_name ஆகியவற்றை ரீட் செய்யவும் மதிப்பிருத்தவும் பயன் படுகின்றன.

Read only properties


ஒரு கிளாஸில் ஒரு field –யை ரீட் ஒன்லியாக மாற்றுவதற்கு அதன் ப்ராப்பர்டியில் கெட் ஆக்சசர் மட்டும் அறிவிப்பதன் மூலம் செய்யலாம்.

சான்று நிரல்:

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

namespace propertyImplementation
{
    class student
    {
        private int _id;
        private string _name;
        private int _passmark = 40;
        public int Passmark
        {
            get
            {
                return this._passmark;
            }
        }
           
            public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student s1 = new student();
            s1.Id = 101;
            s1.Name = "karthikeyan";
            //s1.Passmark = 50;   //illegal can not set value for passmark.

            Console.WriteLine("Id= {0} && name= {1} && passmark={2}", s1.Id, s1.Name,s1.Passmark );
            Console.ReadLine();
        }
    }
}

வெளியீடு:



Auto implemented property.

Public int Id{get;set;}
Public int age {get;set;}
Fields-ன் பெயரை அறிவிக்காமல் வெறும் ப்ராபர்டிகளின் பெயரை நேரடியாக மேலும் கெட் மற்றும் செட் ஆக்சசர்களின் அறிவிப்பை கொடுத்து(அதன் டெஃபினிசன் வராது) செய்வதை auto mapped property எனப்படுகின்றது.

சான்று நிரல்:

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

namespace propertyImplementation
{
    class student
    {
        public int Id { get; set; }
        public int Age { get; set; }

    }
    class Program
    {
        static void Main(string[] args)
        {
            student s1 = new student();
            s1.Id = 101;
            s1.Age = 18;
           

            Console.WriteLine("Id= {0} && age={1}",s1.Id,s1.Age);

            Console.ReadLine();
        }
    }
}

வெளியீடு:

 

மதிப்பை பரிசோதணை செய்தல்:

மெத்தட்களில் செய்வது போல் ப்ரொபெர்டிகளில் நாம் கொடுக்கும் இன்புட் பரிசோதணை (validate)  செய்ய முடியும்.குறிப்பிட்ட மதிப்பை மட்டும் அனுமதிப்பதற்கு இதனை உபயோக படுத்தல்லாம்.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace propertyImplementation
{
    class student
    {
        private int id;
        private int age;

        public int Id
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
            }
        }
        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {

                if (value < 16)
                {
                    Console.WriteLine("invalid student age");
                    this.age = 0;
                }
                else
                {
                    this.age = value;
                }


            }



        }
        class Program
        {
            static void Main(string[] args)
            {
                student s1 = new student();
                s1.Id = 101;
                s1.Age = 15;


                Console.WriteLine("Id= {0} && age={1}", s1.Id, s1.Age);

                Console.ReadLine();
            }
        }

    }
}

வெளியீடு:

 

                         -முத்து கார்த்திகேயன்,மதுரை.



ads Udanz

No comments:

Post a Comment