c# - Confused over access to private variable from Linq query -


i'm bit confused on why have access private variable linq query?

the access in propertyname method , accessing _propertyname variable. thought since private wouldn't have access it?

public class objectgraph {     private readonly string _propertyname;      public string propertyname     {                 {             // ermmm, not sure why select can access _propertyname???             var parents = string.join("/", parents.select(p => p._propertyname));             return string.format("{0}/{1}", parents, _propertyname);         }     }      public objectreplicationcontext source { get; private set; }      public int closestparentid { get; set; }      public bool traversechildren { get; set; }      public list<objectgraph> parents { get; set; }      public objectgraph(object source, string propertyname)     {         source = new objectreplicationcontext(source);         _propertyname = propertyname;          parents = new list<objectgraph>();         traversechildren = true;     } } 

a class can access own private members. lambda anonymous callback method exists within class; syntactic sugar provided c# compiler.

imagine had instead

private static string getproperyname(objectgraph obj) {   return obj._propertyname; }  public string propertyname {       {         // ermmm, not sure why select can access _propertyname???         var parents = string.join("/", parents.select(getproperyname));         return string.format("{0}/{1}", parents, _propertyname);     } } 

this equivalent code.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -