有条件可选参数以消除重载

发布于 2025-01-04 08:13:55 字数 1425 浏览 5 评论 0原文

我有以下 C# 方法:

public Vector2 GetVectorToTile(int x, int y)
    {
        return new Vector2(x * TileWidth, y * TileHeight);
    }
public Vector2 GetVectorToTile(Point start)
    {
        return GetVectorToTile(start.X, start.Y);
    }

第二个方法以非常简单的方式重载第一个方法。然而,我真的不喜欢这样的“输入清理程序过载”——我觉得不应该有一个单独的方法来转换每种可能的输入类型。

现在,如果第一个方法的输入是单个 Vector2 而不是两个数字,我可以使用条件参数,这样如果参数是 Point 而不是 Vector2< /code>,它应该首先从 Point 转换为 Vector2,然后照常进行。

然而,事实并非如此。

所以我的问题是,如何告诉该方法接受“两个整数或一个点”,然后在计算结果之前将后者转换为前者?

我可以在 Matlab 中以一种人为的方式做到这一点,但它看起来与 C# 完全无关:

function result = VectorToTile(varargin)
    x = 0;
    y = 0;

    if size(varargin{1}) == [1, 1]
        disp('Assuming Vector input!');
        x = varargin{1}{1}.x; % Assuming the "Vector2" equivalent is a struct with .x and .y
        y = varargin{1}{1}.y;
    else
        disp('Assuming integer pair input!');
        x = varargin{1}{1};
        y = varargin{1}{2};
    end

    result.x = x * 32; % An example value for TileWidth
    result.y = y * 32; % An example value for TileHeight
end

这将与以下两个输入一起使用:

ints{1} = 25;
ints{2} = 125;
VectorToTile(ints);

vect{1}.x = 25;
vect{1}.y = 125;
VectorToTile(vect);

它说明了我想要做的事情,但不幸的是没有真正的 varargin 在 C# 中,也不是所有内容都被视为矩阵。

I have the following C# methods:

public Vector2 GetVectorToTile(int x, int y)
    {
        return new Vector2(x * TileWidth, y * TileHeight);
    }
public Vector2 GetVectorToTile(Point start)
    {
        return GetVectorToTile(start.X, start.Y);
    }

The second method overloads the first in a very simple manner. However, I don't really like such an "input sanitizer overload"- I feel that there shouldn't be a separate method for converting each possible input type.

Now if the input of the first method was a single Vector2 instead of two numbers, I could use conditional arguments such that if the argument is Point instead of Vector2, it should first convert from Point to Vector2 and then proceed as usual.

However, that is not the case.

So my question is, how can I tell the method to accept "EITHER two integers OR a single point", and then convert the latter into the former before computing a result?

I can do this in a contrived manner in Matlab, but it looks completely unrelated to C#:

function result = VectorToTile(varargin)
    x = 0;
    y = 0;

    if size(varargin{1}) == [1, 1]
        disp('Assuming Vector input!');
        x = varargin{1}{1}.x; % Assuming the "Vector2" equivalent is a struct with .x and .y
        y = varargin{1}{1}.y;
    else
        disp('Assuming integer pair input!');
        x = varargin{1}{1};
        y = varargin{1}{2};
    end

    result.x = x * 32; % An example value for TileWidth
    result.y = y * 32; % An example value for TileHeight
end

This will work with the following two inputs:

ints{1} = 25;
ints{2} = 125;
VectorToTile(ints);

vect{1}.x = 25;
vect{1}.y = 125;
VectorToTile(vect);

It illustrates what I want to do, but unfortunately there isn't really a varargin in C#, nor is everything treated as a matrix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最美不过初阳 2025-01-11 08:13:55

我不想显得轻率,但是:

所以我的问题是,如何告诉该方法接受“两个整数或一个点”,然后在计算结果之前将后者转换为前者?

像这样:

public Vector2 GetVectorToTile(int x, int y)
{
    return new Vector2(x * TileWidth, y * TileHeight);
}
public Vector2 GetVectorToTile(Point start)
{
    return GetVectorToTile(start.X, start.Y);
}

您发布的代码不正是您想要的吗?并且以比您发布的 matlab 示例更干净、更类型安全的方式..(恕我直言)

I don't want to seem flippant but:

So my question is, how can I tell the method to accept "EITHER two integers OR a single point", and then convert the latter into the former before computing a result?

like this:

public Vector2 GetVectorToTile(int x, int y)
{
    return new Vector2(x * TileWidth, y * TileHeight);
}
public Vector2 GetVectorToTile(Point start)
{
    return GetVectorToTile(start.X, start.Y);
}

isn't the code you posted doing exactly what you want? and in a much cleaner more typesafe way than in the matlab example you posted.. (IMHO)

梦里梦着梦中梦 2025-01-11 08:13:55

可以

public Vector2 GetVectorToTile(int? x = null, int? y = null, Point? start = null)  
{
    Vector2 vector = null;
    if (x.HasValue && y.HasValue)
    {
        vector = new Vector2(x * TileWidth, y * TileHeight);         
    }
    else if(start.HasValue)
    {
        vector = new Vector2(start.X * TileWidth, start.Y * TileHeight); 
    }

    return vector;
}

但是同意 Sam Holder 的观点,我认为你在 C# 中所做的很好。上面的内容看起来很混乱,并且可能比您的方法更容易出现错误。例如,如果您提供了全部 3 个参数,哪一个参数优先呢? ETC。

you could:

public Vector2 GetVectorToTile(int? x = null, int? y = null, Point? start = null)  
{
    Vector2 vector = null;
    if (x.HasValue && y.HasValue)
    {
        vector = new Vector2(x * TileWidth, y * TileHeight);         
    }
    else if(start.HasValue)
    {
        vector = new Vector2(start.X * TileWidth, start.Y * TileHeight); 
    }

    return vector;
}

But agree with Sam Holder, I think what you're doing is fine in C#. The above looks messy and probably more prone to bugs than your approach. E.g., what if you provided all 3 arguments, which would take precedence? etc.

-残月青衣踏尘吟 2025-01-11 08:13:55

您可以使用参数数组 params 关键字来传入对象数组。

例如:

public Vector2 GetVectorToTile(params object[] args)

然后您可以检查数组中有多少个并进行相应处理。

但是强类型参数肯定更好吗?

You could use a parameter array params keyword to pass in an object array.

e.g.:

public Vector2 GetVectorToTile(params object[] args)

Then you can check how many is in your array and process accordingly.

But surely strongly typed parameters are better?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文