返回字段值的结构,其语法与(例如)Int 所用的语法相同

发布于 2024-11-30 22:24:07 字数 678 浏览 0 评论 0原文

我可能只是感到困惑,如果我知道得更好的话就不想这样做,但这里是:

当你有 int myInt = 5; 时,你可以通过以下方式访问该 int 的值:调用变量的名称:int myOtherInt = myInt + 1;myInt ++;

我有一个结构,我想以这种方式表现,并返回现有的值类型。

简而言之,该结构体有一个 Func 字段,并提供该函数的结果;它还具有最小/最大字段,可用于限制输出,无论函数的原始结果如何。

Func<Foo, int> _func;
int _min;
int _max;

public MyStruct(Func<Foo, int> func, int min, int max) {...}

// Doesn't work
public int this 
{ 
    get { return Clamp(_min, _max, _func()); }
}

Clamp(min, max, val) {}

所以,我希望能够写:

var myStruct = new MyStruct((myFoo => myFoo.IntVal * 5), 1, 1000);
int myInt = myStruct + 5;

It's likely that I'm just confused and wouldn't want to do this if I knew better, but here goes:

When you have int myInt = 5; you can access the value of that int just by calling the name of the variable: int myOtherInt = myInt + 1; or myInt ++;.

I have a struct that I want to behave this way, and return an existing value type.

In short, the struct has a Func field, and provides the result of that function; it also has Min/Max fields you use to clamp the output regardless of the raw result of the function.

Func<Foo, int> _func;
int _min;
int _max;

public MyStruct(Func<Foo, int> func, int min, int max) {...}

// Doesn't work
public int this 
{ 
    get { return Clamp(_min, _max, _func()); }
}

Clamp(min, max, val) {}

So, I want to be able to write:

var myStruct = new MyStruct((myFoo => myFoo.IntVal * 5), 1, 1000);
int myInt = myStruct + 5;

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

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

发布评论

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

评论(4

暮光沉寂 2024-12-07 22:24:07

这里有两种选择供您选择。您可以定义隐式转换运算符,也可以定义每个您感兴趣的算术运算符

如下使用隐式强制转换运算符的完整示例。不需要实现 this 属性。

using System;

public struct MyStruct
{
    public static implicit operator int(MyStruct myStruct)
    {
        return 2;
    }
}

public class Test
{
    public static void Main(string[] args)
    {
        var myStruct = new MyStruct();
        var myInt = myStruct + 5;
        Console.WriteLine("Int value: {0}", myInt);
    }
}

在本例中,myStruct + 5 使用您已实现的重载运算符将您的结构隐式转换为整数。

There are two options for you here. You can define an implicit casting operator, or you can define each of the arithmetic operators you're interested in.

Here's a full example of using the implicit cast operator. No this property needs to be implemented.

using System;

public struct MyStruct
{
    public static implicit operator int(MyStruct myStruct)
    {
        return 2;
    }
}

public class Test
{
    public static void Main(string[] args)
    {
        var myStruct = new MyStruct();
        var myInt = myStruct + 5;
        Console.WriteLine("Int value: {0}", myInt);
    }
}

In this case, myStruct + 5 implicitly casts your struct into an integer, using the overloaded operator you've implemented.

满身野味 2024-12-07 22:24:07

您应该检查隐式运算符:
http://msdn.microsoft.com/en- us/library/z5z9kes2(v=vs.71).aspx 它有局限性,但可以
让你走得更远。

一篇很好的文章,介绍了示例及其局限性之一:
http://www.markhneedham.com/blog/2009/ 02/22/c-隐式运算符/

You should check out the Implicit operator:
http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.71).aspx it has limitations, but could
get you a long way.

A nice post that does into examples and one of it's limitations:
http://www.markhneedham.com/blog/2009/02/22/c-implicit-operator/

念三年u 2024-12-07 22:24:07

您可以通过实现公共静态隐式运算符 int(MyStruct astruct) 来实现此目的(请参阅http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.71).aspx 的更多详细信息。)以及运算符 +(请参阅http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx 获取教程)。

public static MyStruct operator +(MyStruct astruct, int IntVal)
{
    // code to add an int to the underlying value of your struct
}

public static implicit operator int(MyStruct astruct)
{
    return return Clamp(_min, _max, _func());
}

这样,我认为

int myInt = myStruct + 5;

应该可以工作,为 myStruct + 5 调用 operator + ,然后为 myInt = 调用 implicit operator int代码>部分。

You could do this by implementing public static implicit operator int(MyStruct astruct) (see http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.71).aspx for more details.) and also an operator + (see http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx for a tutorial).

public static MyStruct operator +(MyStruct astruct, int IntVal)
{
    // code to add an int to the underlying value of your struct
}

public static implicit operator int(MyStruct astruct)
{
    return return Clamp(_min, _max, _func());
}

with that, I think

int myInt = myStruct + 5;

should work, calling operator + for myStruct + 5 and then implicit operator int for the myInt = part.

情泪▽动烟 2024-12-07 22:24:07

为什么不直接重载运算符 +(MyStruct a, int b)

教程:http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx

Why not just overload the operator +(MyStruct a, int b)

A tutorial: http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx

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