C# | Properties
முத்லில் properties ஏன் அறிமுகப்படுத்தப்பட்டது எனப்
பார்ப்போம்.
ஒரு கிளாஸின் மெம்பர்கள் private ஆக இருந்தால் மற்ற கிளாஸில்
அதை ஆக்சஸ் செய்ய இயலாது. குறிப்பாக fields.ஆனால் அதை public ஆக அறிவித்தால் oops
கான்சப்ட்ஸ் அடிப்ப்டையான encapsulation என்பதை மீறியதாகும். அதாவது ஒரு கிளாஸின்
டேட்டா மெம்பர்கள் எப்பொழுதும் மற்ற கிளாஸில் நேரடியாக அனுக கூடாது.
சான்று நிரல்
| 
// C#
  program to illustrate the problems  
// with
  public and private members  
using System;  
//
  public class  
public class C1  
{  
    //
  public data members  
    public int rn;  
    public string name;  
    //
  private field  
    //
  private int marks = 35;  
}  
//
  another public class  
public class C2  
{  
// Main
  Method  
public static void Main(string[] args)  
{  
    //
  Creating object of C1 class  
    C1
  obj = new C1();  
    //
  setting values to public  
    //
  data members of class C1  
    obj.rn
  = 10000;  
    obj.name
  = null;  
    //
  setting values to private  
    //
  data members of class C1  
    //
  obj.mark = 0;  
    //
  display result  
    Console.WriteLine("Name: 
  {0} \nRoll No: {1}", obj.name, obj.rn);  
}  
}  | 
வெளியீடு:
Name:   
Roll No: 10000
Explanation:மேலே உள்ள சான்று நிரலில் c1 என்கின்ற கிளாஸின் public
மெம்பர்களை c2-வில் அனுகியுள்ளோம். முதலில் c2 கிளாஸிற்கு obj என்கின்ற பெயரில்
ஆப்ஜெக்ட் உருவாக்கினோம் பிறகு பப்ளிக் மெம்பர்களை அந்த ஆப்ஜெக்ட் பெயர் கொண்டு
ஆக்சஸ் செய்தோம் .இங்கு marks என்கின்ற ஃபீல்டை அனுக முடியாது ஏனெனில் அது
பிரைவேட்.
ஒரு கிளாஸின் ப்ரைவேட் மெம்பர்களை
மற்ற கிளாஸில் நேரடியாக அணுக வேண்டும். அதே  நேரத்தில் அது பப்ளிக் ஆக இருக்கவும்
கூடாது.
பிராப்பர்ட்டிஎன்பது சிறப்பு டேட்டா மெம்பர் ஆகும். அதைக்
கொண்டு ஒரு பிரைவேட் ஃபில்டுகளை பெறவும் மற்றும் மதிப்பிருத்துதலும் முடியும். அவை
get மற்றும் set ஆக்சசர்களை கொண்டுள்ளன. Get ஆக்சசர் மூலம் ஒரு ப்ரைவேட் ஃபீல்டை
ரீட் செய்ய முடியும். Set ஆக்சசர் மூலம் ஒரு பிரைவேட் ஃபீல்டிற்கு மதிப்ப்ருத்த
முடியும்.  Encapsulation மற்றும் தகவலை
மறைத்தலும் பிராப்பர்ட்டிகளின் மூலம் சாத்தியமே.
 Accessors: “set” மற்றும் and “get” என்பது
“Accessors” ஆகும். ஒரு பிராப்பர்ட்டியை வெறும் ரீட் செய்யவோ அல்லது வெறும்
மதிப்பிருத்தவோ முடியும். இரண்டு வகையான ஆக்சசர்கள் உள்ளன அவை set மற்றும் get
ஆகும்.  
          Read and Write Properties: இரண்டு
ஆக்சசர்களும் பிராப்பர்ட்டியில் உள்ளது.
          Read-Only Properties: get
accessor  மட்டும் இருக்கும்.
       Write Only Properties: set accessor  மட்டும் இருக்கும்.
     Auto Implemented
Properties:இது மேலும் கூடுதல் லாஜிக் இல்லாத
போது இது பயன்படுகின்றது. இது c# 3.0-வில் அறிமுகப்படுத்தப்பட்டது. 
சிண்டாக்ஸ்
<access_modifier>
<return_type> <property_name>
{
        get { // body }
        set { // body }
<access_modifier>
ஆனது public,
private, protected அல்லது internal ஆக இருக்கலாம். <return_type>என்பது c#
டேட்டா டைப்பில் ஏதாவது ஒன்றாக இருக்கலாம் get மற்றும் set ஆக்சசர் இரண்டும்
வெவ்வேறு ஆக்சஸ் மாடிஃபைர் ஆக இருக்கலாம்.static மாடிஃபையர் கொண்டு static
property அமைக்கலாம். Virtual கீவேர்டு கொண்டு virtual property அமைக்கலாம். 
Get Accessor:இது
மட்டும் இருந்தால் இது ரீட் ஒன்லி ப்ராப்பட்டி ஆகும். 
சான்று
நிரல்
class
Geeks {
//
Declare roll_no field
private
int roll_no;
//
Declare roll_no property
public
int Roll_no 
{
   get 
     {
      return roll_no;
     }
}
}
- Set Accessor: இது மட்டும் இருந்தால் இது ரைட் ஒன்லி பிராப்பர்ட்டி ஆகும்.
- சான்று நிரல்.
class
Geeks {
//
Declare roll_no field
private
int roll_no;
//
Declare roll_no property
public
int Roll_no
 { 
   get 
     {
         return roll_no;
      }
   set 
     {
         roll_no = value;
      }
}
}
Accessor Accessibility
- இண்டர் ஃபேஸில் ஆக்சஸ் மாடிஃபயர் அறிவிக்க இயலாது.
- இரண்டு ஆக்சசர்களும் இருந்தால் மட்டுமே ஆக்சஸ் மாடிஃபயர் அறிவிக்க முடியும்.
சான்று நிரல்:
Program 1: ரீட் ஒன்லி
பிராப்பர்ட்டி
| 
// C#
  program to illustrate the  
//
  read-only property  
using System;  
public class Student {  
    //
  Declare counter field as cnt  
    private static int cnt;   
    //
  to define constructor  
    public Student()  
    {
   
        //
  increment the counter   
        //
  using constructor  
        cnt++; 
   
    }
   
    //
  Declare counter property  
    public static int Counter   
    {
   
        //
  read-only property  
        get 
        {
   
            return cnt;  
        }
   
    }
   
}  
class StudentTest {  
    //
  Main Method  
    public static void Main(string[] args)  
    {
   
        //
  create three instances of  
        //
  Student class it call constructor  
        //
  three times which increase the counter  
        Student
  s1 = new Student();  
        Student
  s2 = new Student();  
        Student
  s3 = new Student();  
        //
  s1.Counter = 10;  
        //
  Compile Time Error: Can't set value of  
        //
  Counter because it is read only.  
        Console.WriteLine("Total
  No of Student: " + Student.Counter);  
        //
  Program Give Warning  
        //
  The variable `s1' is assigned but its value is never used  
    }
   
}  | 
வெளியீடு:
Total No of Student: 3
Program 2: “get”மற்றும் “set”
accessors இரண்டும்.
| 
// C#
  program to illustrate the   
// read
  and wirte property  
using System;  
public class Student {  
    //
  Declare name field  
    private string name =
  "GeeksforGeeks";   
    //
  Declare name property  
    public string Name   
    {
   
       get 
        {
   
            return name;  
        }
   
        set 
        {
   
            name
  = value;  
        }
   
    }
   
}  
class TestStudent {  
    //
  Main Method  
    public static void Main(string[] args)  
    {
   
        Student
  s = new Student();  
        //
  calls set accessor of the property Name,  
        //
  and pass "GFG" as value of the  
        //
  standard field 'value'.  
        s.Name
  = "GFG";  
        //
  displays GFG, Calls the get accessor  
        //
  of the property Name.  
        Console.WriteLine("Name:
  " + s.Name);  
    }
   
}  | 
Output:
Name: GFG
C# | Indexers
இது ஒரு கிளாஸின் ஆப்ஜெக்டை அர்ரே ஆக உபயோகிக்க இயலும்.ஒரு கிளாஸில் indexer இருந்தால் அந்த கிளாஸ் ஆனது ஒரு விர்ச்சுவல் அர்ரே போல் செயல்படுகின்றது.இதுவும் பிராப்பர்ட்டி போன்றே எழுதப்படுகின்றது. இரண்டுக்கும் உள்ள முக்கிய வித்தியாசம் இண்டெக்சர் பராமீட்டர்கள் இருக்கும்.Syntax:
[access_modifier] [return_type] this [argument_list]
{
  get 
  {
     // get block code
  }
  set 
  {
    // set block code
  }
}மேலே உள்ளதில்
- access_modifier: இது public, private, protected அல்லது internal.
- return_type: ஏதாவது ஒரு C# டேட்டா டைப்.
- this: கரண்ட் கிளாஸின் ஆப்ஜெக்டை சுட்டு நோக்குகின்றது
- argument_list: பரா மீட்டர் லிஸ்ட்
- get{ } and set { }: accessors.
| // C# program to illustrate the Indexer usingSystem;  // class declaration classIndexerCreation  {      // class members     privatestring[] val = newstring[3];           // Indexer
  declaration     // Here pubic is
  the modifier     // string is the
  return type of      // Indexer and
  "this" is the keyword     // having
  parameters list     publicstringthis[intindex]      {          //
  get Accessor         //
  retrieving the values          //
  stored in val[] array         //
  of strings         get        {
               returnval[index];         }
           //
  set Accessor         //
  setting the value at          //
  passed index of val         set        {
               //
  value keyword is used             //
  to define the value              //
  being assigned by the             //
  set indexer.              val[index]
  = value;         }
      } }  // Driver Class classmain {          // Main Method     publicstaticvoidMain() {                  //
  creating an object of parent class which         //
  acts as primary address for using Indexer         IndexerCreation
  ic = newIndexerCreation();
           //
  Inserting values in ic[]         //
  Here we are using the object         //
  of class as an array         ic[0]
  = "C";         ic[1]
  = "CPP";         ic[2]
  = "CSHARP";          Console.Write("Printing
  values stored in objects used as arrays\n");                  //
  printing values          Console.WriteLine("First
  value = {0}", ic[0]);         Console.WriteLine("Second
  value = {0}", ic[1]);         Console.WriteLine("Third
  value = {0}", ic[2]);          } }  | 
Printing values stored in objects used as arrays
First value = C
Second value = CPP
Third value = CSHARPமுக்கிய குறிப்புகள்
- இரண்டு வகையான இரண்டு இண்டெக்சர்கள் உள்ளன அவையான rs i.e. One Dimensional Indexer மற்றும் MultiDimensional Indexer.
- Indexers ஆனதை ஓவர்லோட் செய்ய இயலும்
- இது பிராப்பர்ட்டியிலிருந்து மாறுபடுகின்றது
- இது ஒரு ஆப்ஜெக்டை அர்ரே போல் அனுக பயன்படுகின்றது.
- this கீவேர்டு ஒரு இண்டெக்சரை அறிவிக்க அறிவிக்க பயன்படுகின்றது.
- இண்டெக்சர் ஆனது smart array என அறியப்படுகின்றது.
- இது ஸ்டேட்டிக் மெம்பராக இருக்க இயலாது.
-முத்து கார்த்திகேயன்,மதுரை.
 
 

 
 
No comments:
Post a Comment