difference between property and method in .net

Posted by Shanky11 under C# on 1/29/2013 | Points: 10 | Views : 5170 | Status : [Member] | Replies : 2
difference between property and method in .net
if any one knows this by practically .............




Responses

Posted by: Sarithapraveen on: 1/29/2013 [Member] Starter | Points: 25

Up
0
Down
For accessing a variable outside the class we declare that variable as Public. Properties are used to access a private variable declared in a class to outside that class. Declaring a property as public using get and set, we can access that private variable from anywhere. So we can avoid the use of Public Variables.

Eg: Class Employee
{
private int employeeid
public int EmployeeId
{
get { return employeeid; }
set { employeeid= value; }
}
}



//from other forms create an object of class employee
Employee objEmp = new Employee();
int id = objEmp.EmployeeId;

A method is a code block containing a series of statements. Methods must be declared within a class or a structure. It is a good programming practice that methods do only one specific task. Methods bring modularity to programs














Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kamilacbe on: 1/29/2013 [Member] Starter | Points: 25

Up
0
Down
hi,

I would like to say that was it a getter./setter or may be method it depends on the scenario you travel in your code, so they do the same job with some limitation on their own, for eg
if your wanna retrieve a data from DB on that case you can go for a method which returns data , or if u wanna just display the interval value which just retrieves from memory on that case properties would do a great job .

for eg

public class A
{
private int mynum ;

public int num
{
get{return mynum;} // retrieves the current obj state
set{ mynum = value;} // assigns the value to the current state
}





}



Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response