c# - Is there a performance or encapsulation reason to create a private variable to use in place of a public property? -


let's have class public property used property injection, this:

public partial class managefoo : pagebase {     [inject]     public ifoo foo { get; set; }      public void somemethod()     {         this.foo.bar();     } } 

our ioc container handles injecting instance of ifoo property. can see property foo uses short hand { get; set; } define property. have heard bad practice use public property throughout class , should have accompanying private variable this:

public partial class managefoo : pagebase {     [inject]     public ifoo foo     {         { return _foo; }         set { _foo = value; }     }      private ifoo _foo;      public void somemethod()     {         _foo.bar();     } } 

i've heard differing opinions i've yet hear concrete reasons why 1 better another. shorter syntax preferred or private variable preferred, , why?

there's no significant difference between using automatically-implemented property , using explicit form. yes, class's code go through property - going inlined jit.

the automatically implemented property gives less code, , it's clearer - go it. next time tells it's bad practice refer public property within class declares it, ask concrete examples of how can harmful :)


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -