Encapsulation C# Best practices -


just clarification , code practices. understand concept of encapsulation, can tell me difference between these 2 codes , in scenario use them. thanks. ps: not looking links answers, want honest opinion.

code 1:

class program {     static void main(string[] args)     {         car objcar = new car();         printvehicledetails(objcar);     }      private static void printvehicledetails(vehilce vehicle)      {         console.writeline("here vehicles' details: {0}", vehicle.formatme());     } }  abstract class vehilce {     protected string make { get; set; } //here     protected string model { get; set; } //here      public abstract string formatme();  }  class car : vehilce {      public override string formatme()     {          return string.format("{0} - {1} - {2} - {3}", make, model);     } } 

code 2:

class program {     static void main(string[] args)     {         car objcar = new car();         printvehicledetails(objcar);     }      private static void printvehicledetails(vehilce vehicle)      {         console.writeline("here vehicles' details: {0}", vehicle.formatme());     } }  abstract class vehilce {     public string make { protected get; protected set; } //here     public string model { protected get; protected set; } //here      public abstract string formatme();  }  class car : vehilce {      public override string formatme()     {          return string.format("{0} - {1} - {2} - {3}", make, model);     } } 

there common approach: separate data , logic. in case should make properties public (maybe private setters) , put formatting somewhere else, example, in extension method.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -