What is the method of using the {get, set} property in C # 3.0

Possible duplicate:
C # auto properties

Hello,
I recently switched to the .net (C #) platform from Java. Here, I donโ€™t yet have a big problem ... anyway, I messed up the method

  property {get, set} 
. In fact, I could not get the exact meaning of these (properties) for me, the same task of initializing variables or the field or state of an object can be done using methods. where we can declare fields as private and have access to them using a public method of the same class.

Well, one simple thing, I'm not a programmer or employee, a temporary career.

- so thank you all for your help to help me.

property {get, set}
+4
source share
6 answers

Using properties removes method calls to set values โ€‹โ€‹and get their values โ€‹โ€‹for private members. How:

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, the number will set the number = 100, and the next line will display the number, which is 100.

+6
source

For encapsulation purposes, the fields of objects must be hidden from others, sometimes the object wants to transmit some data, this data can be field data for some changes or can be obtained from several field data, and the object can do this by property.

+4
source

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

The property tells the caller that the value is more or less available instantly. When the caller needs myObject.CustomerList , he expects the list is already loaded and cached by his class in the field. He expects that he does not need to save the list in a local variable when performing further operations on it.

When he needs to do myObject.GetCustomerList() , he expects that it will take some time, since the list will be retrieved from somewhere, and that he will probably get another copy of the list every time he calls, and that he thus , save the list in a local variable.

+4
source

The resulting part of the property is the part in which you retrieve the value of the property.

The property part set is the part in which you can assign a value to the property.

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

+3
source

The following code snippet is

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

equivalent to the 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 ahead, you can get the same effect using only the following line:

 public string Name { get; set; } 

This is called an automatic property in which the compiler creates a fallback private field and getter / setter for this property. Hope this helps.

+3
source

C # properties are similar to getter and setter functions in Java.
In fact - they are compiled into MSIL methods.

+2
source

All Articles