问题
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