difference b/w properties and method in .net

Posted by Shanky11 under Interview Questions on 2/1/2013 | Points: 10 | Views : 1094 | Status : [Member] | Replies : 3
difference b/w properties and method in .net
what is the actual defference between properties and method




Responses

Posted by: Kundan64 on: 2/1/2013 [Member] Starter | Points: 25
Posted by: Ranjeet_8 on: 2/1/2013 [Member] [MVP] Gold | Points: 25

Up
0
Down
Properties are used to represent data, whereas methods are used to performs actions.
Properties are created by using getter and setter. get{} and set{} but methods create like public void method1(parameter list here)

Go through this url-
http://www.codersource.net/MicrosoftNet/CBasicsTutorials/CNetmethodsandproperties.aspx

Please Mark As Answer if this helps you

.

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

Posted by: Kusumas on: 2/4/2013 [Member] Starter | Points: 25

Up
0
Down
Properties
Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. In other languages, this is often accomplished by programs implementing specialized getter and setter methods. C# properties enable this type of protection while also letting you access the property just like it was a field.

Another benefit of properties over fields is that you can change their internal implementation over time. With a public field, the underlying data type must always be the same because calling code depends on the field being the same. However, with a property, you can change the implementation. For example, if a customer has an ID that is originally stored as an int, you might have a requirements change that made you perform a validation to ensure that calling code could never set the ID to a negative value. If it was a field, you would never be able to do this, but a property allows you to make such a change without breaking code. Now, lets see how to use properties.

Example:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}

Method:

Methods are extremely useful because they allow you to separate your logic into different units. You can pass information to methods, have it perform one or more statements, and retrieve a return value. The capability to pass parameters and return values is optional and depends on what you want the method to do. Here's a description of the syntax required for creating a method:

attributes modifiers return-type method-name(parameters )

{

//statements

}

The return-type can be any C# type. It can be assigned to a variable for use later in the program. The method name is a unique identifier for what you wish to call a method. To promote understanding of your code, a method name should be meaningful and associated with the task the method performs. Parameters allow you to pass information to and from a method. They are surrounded by parenthesis. Statements within the curly braces carry out the functionality of the method.

Example:

class Motorcycle
{
public void StartEngine() { }
public void AddGas(int gallons) { }
public int Drive(int miles, int speed) { return 0; }
}




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

Login to post response