I make extensive use of inheritance, polymorphisim, and encapsulation but i just realised that i didn't know the following behavior about scope of an object vs a variable. The difference is best shown with code:
public class Obj
{
public string sss {get; set;}
public Obj()
{
sss = "0";
}
}
public partial class testScope : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Obj a = new Obj();
String sss = "0";
Context.Response.Write(sss); // 0
FlipString(sss);
FlipString(a.sss);
Context.Response.Write(sss + a.sss); // 0 0
FlipObject(a);
Context.Response.Write(a.sss); // 1
Context.Response.End();
}
public void FlipString(string str)
{ str = "1"; }
public void FlipObject(Obj str)
{ str.sss = "1"; }
}
So I thought that when a variable is passed into a method that changes are limited to the scope of the method. But it appears that if an object is passed into a method that changes to it's properties extend beyond the method.
I could accept this if the rule was that this behavior exists for object and not variables but in .net everything is an object, a string (like the one in the example is System.String) So whats the rule, how can i predict the scope of a parameter that i pass into a method?