开发者

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

This question already has answers h开发者_如何学Pythonere: Closed 12 years ago.

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}


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.


The following code snippet-

private string name;
public string Name
{
get { return name; }
set { name = value; }
}

is equivalent to following getter and setter in Java:

private String name;
public String getName() { return name; }
public void setName(String newName) { name = newName; }

In C# 3.0 onward, you can have the same effect using only the following line:

public string Name { get; set; }

This is known as automatic property where compiler generates a backing private field and getter/setter for this property. Hope this helps.


For encapsulation purposes, object's fields must be hidden from the others, sometimes object want to pass some data to out, these data can be fields data by some changes, or can be resulted from multiple fields data, and object can do this by Property.


To add to the other answers, I think the question is more about when and why to use properties instead of methods.

A property communicates to the caller that a value is more or less instantly available. When the caller has to do myObject.CustomerList, he expects that the list has already been loaded and is cached by its class in a field. He expects that he doesn't need to store the list in a local variable when doing further operations on it.

When he has to do myObject.GetCustomerList(), he expects that it will take a while as the list would be retrieved from somewhere, and that he will maybe get another instance of the list every time he does the call, and that he thus should store the list in a local variable.


The get portion of a property is the portion where you retrieve the value of a property.

The set portion of a property is the portion where you can assign a value to the property.

Here is a tutorial: http://www.csharp-station.com/Tutorials/Lesson10.aspx


C# Properties are like getter and setter functions in Java.
In fact - they are being compiled to MSIL methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜