create custom constant suffix in C#

与世无争的帅哥 提交于 2021-02-05 06:24:44

问题


i am trying to develop a class for unlimited size integer values, all i need is to make a new custom constant suffix used with the assign operator.

For example:

lets assume the class name is BigInt and the Suffix created is B

the assign statement will be like this

// B character will tell the compiler about the New Data Type
BigInt x = 111111111111111111111111111111111111111111111111B; 

is there any way to achieve this?

Special Regards


回答1:


No, there isn't any way to do this in C# at this time.

Workarounds: You can check out Nemerle which is a very flexible, meta-programming friendly language based on the C# syntax.




回答2:


There is no way to extend the language directly in the manner you describe.

You could write a .NET language that is C# like and accepts such a specifier, but there is nothing like what you are trying to do.

Note: There is an existing structure for integers of arbitrary size BigInteger - no need to reinvent the wheel.




回答3:


No. The language / compiler do not support this. Something close, which you might want to look into is an implicit conversion operator. That would let you do something like this:

BigInt b = "1234";

public class BigInt
{
    public static implicit operator BigInt(string value)
    {
        return new BigInt {Value = value};
    }
    public string Value { get; private set; }
}



回答4:


Microsoft already did this. It's called a BigInteger. Think it came out with .NET 4.0?

Here's the MSDN link: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

As for extending it like that, I don't think that's possible in C#. You'd have to overload the assignment operator, and that's only do-able in C++ as far as I know.




回答5:


I'm not entirely certain but you can take a look at Roslyn. Check out http://kevinmontrose.com/2012/06/10/extending-type-inferencing-in-c/ for more information.



来源:https://stackoverflow.com/questions/11268213/create-custom-constant-suffix-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!