Solved

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

Posted on 2012-03-30
6
201 Views
Last Modified: 2012-04-16
Kindly quote example.
0
Comment
Question by:searchsanjaysharma
6 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 125 total points
ID: 37790018
Kindly see these:-
http://www.csharp-station.com/Tutorials/Lesson10.aspx
http://www.dotnetperls.com/property

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

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 125 total points
ID: 37793357
0
 
LVL 12

Accepted Solution

by:
Anuradha Goli earned 125 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}

where:

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
{
   get
   {
      return name;
   }
}
Employee e1 = new Employee();
...
Console.Write(e1.Name);   // The get accessor is invoked here
-------------------
public string Name
{
   get
   {
      return name;
   }
   set
   {
      name = value;
   }
}

e1.Name = "Anu";   // The set accessor is invoked here
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 15

Assisted Solution

by:navneethegde
navneethegde earned 125 total points
ID: 37796074
Hi!

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
       get{}

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

Thanks!
0
 

Author Comment

by:searchsanjaysharma
ID: 37850076
ok
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37850078
ok
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 57
Help with C#, MVC, razor. 6 34
Close form "before" open 3 40
Need to start a web service from Visual Studio 2015 Pro 2 24
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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