When we write
int a = 5;
We assign a to a value of 5, but if I want to make a class in which one could enter a very large number by writing
myclass a = 12345678987654321234567898765432123456789;
It is necessary that a class or structure be able to understand the assignment of this set of characters (numbers), is this a feature of the compiler if you cannot do this?

  • BigInteger ? - tym32167
  • In my case, I want to write a number greater than any format that exists, and implicit converts one format to another. - GigSter
  • It is in this form is impossible. - Alexander Petrov
  • @AlexanderPetrov and can you tell if you know why it works for system structures, but not for user structures? Compiler feature? - GigSter
  • Correct your question so that you can clearly understand that you want not an implicit conversion operator, or an assignment operator, but what you want to get is a class with support for numbers that are not supported in Framework. How to ask a good question? - LLENN

3 answers 3

Oh, you have to give your answer.

Rephrased question: how to write a large number of the form 12345678987654321234567898765432123456789 as a literal?

Answer: it is impossible . The reason is a compiler restriction.

By applying an implicit type implicit cast, the maximum can be used ulong.Max .
Using BigInteger and similar classes, you will have to write such large numbers as strings ( string , not numeric literal).

It is possible that you can write an extension to Roslyn API, which will allow the use of such large values. Essentially, make changes to the compiler.

    It is necessary to use operators.
    For example:

     class MyClass { public int A { get; } public MyClass() { } public MyClass(int a) { A = a; } public static implicit operator MyClass(int a) => new MyClass(a); } 

    And, accordingly, it is now possible to use an instance creation construct like this:

     MyClass my = 5; Console.WriteLine(my.A); //5 
    • A similar answer was already (the author deleted it). This is not the answer to the question, because it does not allow the use of large literals of numbers - more than ulong.MaxValue, etc. - Alexander Petrov
    • So what's the problem to use instead of int a - BigInteger? - LiptonDev
    • Good answer. I supplemented it a little with my answer. - Andrew
    • You don't need this at all. Think twenty times before doing this. Most likely you just complicate your life.
    • There is a BigInteger that supports very large numbers. If BigInteger has more than 50 numbers in itself, then only 50 will be returned by .ToString(); If you want more characters to be returned, you need to apply the "R" format. That is: SomeBigInteger.ToString("R"); will return without problems the full value, even if there are more than 50 characters in the figure.

    • But imagine a situation that you really need it. Only you need to have not BigInt, but BigFloat . I would make my class in which 2 BigInt would be stored. And all the internal work would be through the lines (well, the assignment and return of the present value).

    When assigning a value, the string would split into 2 parts and each of the parts would be assigned to its own BigInt as an integer and decimal. It is necessary to write it by the principle, as LiptonDev wrote in his answer.

    Then I would implement my own reloaded methods + - / *

    Here is an example of an overloaded multiplication method:

     public static BigFloat operator *( BigFloat a, BigFloat b ) { //реализация } 

    inside there would be a realization given that our number is divided into a fractional and integer part within your BigFloat class.

    But again, you do not need to do that. And if you think you need it, you are mistaken 100%.

    • My goal was to understand how it works, I needed it for self-development. About biginteger I know. - GigSter