What is get and set and why do we use it.Please quote example

Posted on 2012-03-30
Medium Priority
Last Modified: 2012-04-16
Kindly quote example.
Question by:searchsanjaysharma
LVL 20

Assisted Solution

BuggyCoder earned 250 total points
ID: 37790018
Kindly see these:-

Also read Properties chapter in Jeffery Ritcher's CLR VIA C# book(A Must Read!!!!).....
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 250 total points
ID: 37793357
LVL 12

Accepted Solution

Anuradha Goli earned 250 total points
ID: 37795560
The accessor of a property contains the executable statements associated with getting (reading or computing) or setting (writing) the property.

syntax :

set {accessor-body}
get {accessor-body}


The block that contains the statements to be executed when the accessor is invoked.

private string name;   // the Name field
public string Name   // the Name property
      return name;
Employee e1 = new Employee();
Console.Write(e1.Name);   // The get accessor is invoked here
public string Name
      return name;
      name = value;

e1.Name = "Anu";   // The set accessor is invoked here
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 15

Assisted Solution

Navneet earned 250 total points
ID: 37796074

1.You have a class
class MyClass {}

2.and then you decide to have a variable
class MyClass {
private string myVariable;


3.then you decide to have it public, so it can be accessed publicly
class MyClass {
public string myVariable;

4. Now the one using your public variable, is assigning all type of values
   ex : Your public variable is  
          public int myAge
         any one is assigning values like 500, 1002 values to this varaible, hnec you want to put some check
         and you introduce
        set {}
       which helps you   validate the stuff before assigning the value

        On same path user should be only allowed to set the value and and should not be able to read it.
       or want to let user get the value of your variable in some other form and hence we have

5. Therefore the birth of Property with get{} and set{}


Author Comment

ID: 37850076

Author Closing Comment

ID: 37850078

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question