views:

115

answers:

6

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}

+2  A: 

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

Itay
+1  A: 

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

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

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

Michael Eakins
+1  A: 

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.

Liton
+2  A: 

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.

Anubhav Saini
+2  A: 

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.

Navid Farhadi
+1  A: 

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.

herzmeister der welten