Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Use of Property {get, set} method in C # 3.0 [duplicate]

Possible Duplicate:
C# Automatic Properties

Hello,
I have recently moved towards .net (c#) platform from Java. Here I don't face much problem yet... anyway I 'm messed up with

                   property{get, set}
method. Actually I could not get up the exact meaning of these (property) according to me the same job of initializing variables or field or state of object can be done using methods. where we can declare fields as private and can access them using public method of same class.

           Well one simple thing I am not a programmer or employee but a general students of Computer science aimed to become a programmer as full time career.

- so many thanks all of you in-advance for assisting me.

property{get, set}
like image 537
Nikhil G Avatar asked Nov 29 '22 19:11

Nikhil G


1 Answers

Using Properties removes the method calls for setting values and getting their values for private members. Like:

private int number = 0;
public int Number { get{ return number;} set{number = value;}}

now all you have to do is make an object, and instead of calling functions/methods to access the number, you can do:

ObjectCreated.Number = 100;
Console.WriteLine(ObjectCreated.Number);

implicitly, Number will set number = 100, and next line, would fetch the number which is 100.

like image 129
xxxxxxxxxadfas Avatar answered Dec 05 '22 15:12

xxxxxxxxxadfas