English 中文(简体)
回收用于(say)Int的相同合成物的田地价值
原标题:Struct which returns the value of a field with the same syntax used for (say) an Int
  • 时间:2011-08-21 14:13:39
  •  标签:
  • c#
  • struct

如果我知道情况较好,那么我可能只是混淆不清,而不愿意这样做,但这里是:

如果有<代码>int myInt = 5; 您仅通过打上变量的名称即可获取该星的价值:int my OtherInt = myInt + 1;myInt ++;

我有希望以这种方式行事和归还现有价值的一种障碍。

简言之,该结构有一个富奇领域,提供这一功能的结果;它也有Min/Max油田,无论该功能的原始结果如何,你都用于压缩产出。

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;
最佳回答

这里有两种选择。 http://msdn.microsoft.com/en-us/library/85w54y0a.aspx” rel=“nofollow”>implicitposition,或者你可以界定 运营商对你感兴趣的。

这里是使用含蓄投放器的一个充分例子。 无需执行<代码>。

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 暗中利用你超载的运营商,将你的结构推入了整块。

问题回答

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/

t=p.com>。

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;

应为<密码>工作,请打电话operator + 查询。 Int = part.

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

A tutorial:





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签