C# 2.0 property or the underlying field for assignment?
hi,
got different versions of c# code (2.0, 3.5, 4.0) in same file using preprocessor directives, and that's way here in github: https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/tree/master/source
the question c# 2.0 properties , c# version greater 2.0 automatic properties, both in same file using preprocessor directives say:
#if net_2_0
// c# 2.0 code goes here
#else
// c# 3.0, 3.5 , 4.0 code go here
#endif
here's sample code snippet:
private class parameter : icomparable
{
public parameter(string paramname, string paramvalue)
{
this.parametername = paramname;
this.parametervalue = paramvalue;
}
#if net_2_0
private string paramname;
public string parametername
{
get
{
return this.paramname;
}
set
{
this.paramname = value;
}
}
private string paramvalue;
public string parametervalue
{
get
{
return this.paramvalue;
}
set
{
this.paramvalue = value;
}
}
#else
public string parametername
{
get;
set;
}
public string parametervalue
{
get;
set;
}
#endif
/// <summary>
/// compare name or compare value if both equal
/// </summary>
/// <param name="objectinstance"></param>
/// <returns></returns>
public int compareto(object objectinstance)
{
if (!(objectinstance parameter))
{
throw new invalidcastexception("this object not of type parameter");
}
parameter param = (parameter)objectinstance;
int returnvalue = 0;
if (param != null)
{
returnvalue = this.parametername.compareto(param.parametername);
// if parameter names equal compare parameter values
if (returnvalue == 0)
{
returnvalue = this.parametervalue.compareto(param.parametervalue);
}
}
return returnvalue;
}
}
in case of c# 2.0, can use property assigned in constructor or should use underlying field assigned in constructor? pros , cons of using property assigned in constructor instead of underlying field ?
also, ever property assigned, can use directly property assigned in c# 2.0 or should use preprocessor directives ever assigned?
#if net_2_0
this.paramname = "abc"
this.paramvalue = "xyz"
#else
this.parametername = "abc"
this.parametervalue = "xyz"
#endif
please expertize.
thanks
"in case of c# 2.0, can use property assigned in constructor or should use underlying field assigned in constructor? pros , cons of using property assigned in constructor instead of underlying field ?"
you can assign property. there no need separate code here. the jit inline assignments (since simple property assignment), there's no disadvantage.
in fact, maintaining 2 sets of code doesn't give advantages. you can write code using ".net 2" method have, , use 3.5 , 4.0 0 issues. the auto-property support in later versions of .net doesn't provide advnatages @ runtime - compiler compiles code .net 2 version. since you're writing out, there's absolutely no reason not have single implementation.
suggest using:
private class parameter : icomparable { public parameter(string paramname, string paramvalue) { this.parametername = paramname; this.parametervalue = paramvalue; } // normal styling guidelines .net camelcase fields private string paramname; public string parametername { { return this.paramname; } set { this.paramname = value; } } private string paramvalue; public string parametervalue { { return this.paramvalue; } set { this.paramvalue = value; } } /// <summary> /// compare name or compare value if both equal /// </summary> /// <param name="objectinstance"></param> /// <returns></returns> public int compareto(object objectinstance) { if (!(objectinstance parameter)) { throw new invalidcastexception("this object not of type parameter"); } parameter param = (parameter)objectinstance; int returnvalue = 0; if (param != null) { returnvalue = this.parametername.compareto(param.parametername); // if parameter names equal compare parameter values if (returnvalue == 0) { returnvalue = this.parametervalue.compareto(param.parametervalue); } } return returnvalue; } }
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment