Search code examples
c#inheritancemethodsmultiple-inheritanceaccess-modifiers

C# Inheritance and Methods


I'm learning inheritance and I understand the code below.

namespace InheritanceApplication {
   class Shape {
      public void setWidth(int w) {
         width = w;
      }
      public void setHeight(int h) {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Base class PaintCost
   public interface PaintCost {
      int getCost(int area);
   }

   // Derived class
   class Rectangle : Shape, PaintCost {
      public int getArea() {
         return (width * height);
      }
      public int getCost(int area) {
         return area * 70;
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle Rect = new Rectangle();
         int area;

         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();

         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

However, why have they created the set height and set width functions. Would it not be better practice to simply just do this:

public int width {get;set;}
public int height {get;set;}

and then in the main class just do something like below:

rect.width = 5;
rect.height = 7;

Many thanks,

Amir


Solution

  • I'm sure others will provide different points, but here are my main 2 reasons for using gets/sets. If these don't apply for a given property, chances are I won't use getters/setters.

    1 - Debugging
    It makes it significantly easier to debug data propagation (how data gets passed around) if you can debug a setter that you're concerned about. You can easily throw in a Debug.Print call and debug the value being set if you're concerned it's being passed the wrong value. Or you could place break points and actually debug through the stack trace. For example:

       class Shape {
           public void setWidth(int w) {
               if(w < 0)
                   Debug.Print("width is less than 0!");
    
               width = w;
           }
           public void setHeight(int h) {
               height = h;
           }
           protected int width;
           protected int height;
        }
    

    2 - Value Change Actions
    There may be better ways to achieve this, but I like being able to add simple logic to setters to ensure that any logic that needs to run when a value changes does so. For instance I may use the following:

    public void SetWindowHeight(int newHeight)
    {
        if(WindowHeight == newHeight)
            return;
    
        WindowHeight = newHeight;
    
        UpdateWindowDisplay();
    }
    public int GetWindowHeight()
    {
        return WindowHeight;
    }
    
    private int WindowHeight;
    
    
    public void UpdateWindowDisplay()
    {
        Window.UpdateHeight(WindowHeight);
        // Other window display logic
    }
    

    Although personally I prefer to use property gets/sets, but that's just my preference.

    public int WindowHeight
    {
        get
        {
            return windowHeight;
        }
        set
        {
            if(windowHeight == value)
                return;
    
            windowHeight = value;
    
            UpdateWindowDisplay();
        }
    }
    private int windowHeight;
    
    public void UpdateWindowDisplay()
    {
        Window.UpdateHeight(WindowHeight);
        // Other window display logic
    }