Search code examples
c#libraries

Diiferent types of input in function


I'm making my first library and I want to make different types of input( example: Graphics.DrawLine(); can be determined by four floats or by two points, etc.) How do I make a similar thing?


Solution

  • You just create multiple methods with different arguments.

    For example:

    public class MyGraphics
    {
    
        public bool Draw(Vector2 position, bool big = false)
        {
        }
    
        public void Draw(Line2 line)
        {
        }
        public void Draw(Triangle2 triangle)
        {
        }
    
        public void Draw(Polygon2 polygon)
        {
        }
    
        public void Draw(Line2[] edges)
        {
        }
    }
    

    to be used later as

    {
        MyGraphics g = ...
        Line2 line = ...
        Triangle2 trig = ...
    
        g.Draw(line);  // calls `.Draw(Line2)`
        g.Draw(trig);  // calls `.Draw(Triangle2)`
    }