Hi
Given the following code:-
private int ProductId
{
set { Session["productId"] = value; }
get { return int.Parse(Session["productId"].ToString()); }
}
Would the following code be Getting or Setting the property?
ProductId = int.Parse(Page.RouteData.Values["productid"].ToString());
Thanks
Steven
Loading
VulpesPosted Mar 28, 2012, 9:55 AM
It's setting the property as you're assigning a value to it.
Sam HobbsPosted Mar 28, 2012, 1:37 PM
VulpesPosted Mar 28, 2012, 10:34 AM
The Criteria property is just controlling access to the private criteria field.
This is why you sometimes see properties referred to as 'smart fields'.
For simple properties like this, the JIT compiler will almost certainly replace the property access with a direct access to the underlying field so there's no performance penalty in using properties in this way.
SenthilkumarPosted Mar 28, 2012, 10:24 AM
It is private property and can be accessed with in the class.
The set is used to set the value and get is used to retrieve stored value from the property.
It is integer type property and
ProductId = int.Parse(Page.RouteData.Values["productid"].ToString());
The integer value is assigned to the productId property.
If you want to retrieve the value from the property then it can be
int productIdval = ProductId;
Hence it returns the integer value and not required to the type casting.
But integer value will be stored in the session and it will be stored as object. While getting the value from this integer property they are doing type casting and returns as integer value.
Guest UserPosted Mar 28, 2012, 10:19 AM
Going back on a previous question, given the following:-
I know the following is Getting the value or reference of Criteria property and storing it in the criteria local variable.
var criteria = request.Criteria as CustomerCriteria;
The Criteria property is simply an automatic Get and Set like so:-
private CustomerCriteria criteria;
public CustomerCriteria Criteria
{
Get { return criteria; }
Set { criteria = value; }
}
Does this mean Getting/Returning the Value/Reference is literally returning the criteria field?
Thanks
Steven