Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Integer class in c#?

Tags:

We have Integer class in JAVA, but I couldn't find any equivalent class in C#? Does c# have any equivalent? If not, how do I get JAVA Integer class behavior in c#?

Why do I need this?

It is because I'm trying to migrate JAVA code to c# code. If there is an equivalent way, then code migration would be easier. To addon, I need to store references of the Integer and I don't think I can create reference of int or Int32.

like image 985
Abhishek Avatar asked May 18 '15 06:05

Abhishek


People also ask

Is int a class in C?

int is a type, not a class.

Is integer data type a class?

Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.

Is there an int class in C++?

Is int a class in c++? No. Because int cannot be inherited, cannot have function defined within its scope and lacks so many properties which a class must have.

Is integer a class or object?

Class Integer. The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int .


2 Answers

C# has a unified type system, so int can be implicitly boxed into an object reference. The only reason Integer exists in Java is so that it can be converted to an object reference and stored in references to be used in other container classes.

Since C# can do that without another type, there's no corresponding class to Integer.

like image 97
recursive Avatar answered Sep 22 '22 16:09

recursive


Code migration won´t work out of the box for any type of language without any manual changes. There are things such as a class Integer that simply does not exist within (C# why should it anyway, see recursives answer), so you´d have to do some work on your own. The nearest equivalent to what you´re after is Int32 or its alias int. However you may of course write your own wrapper-class:

    public class Integer     {         public int Value { get; set; }           public Integer() { }         public Integer( int value ) { Value = value; }           // Custom cast from "int":         public static implicit operator Integer( Int32 x ) { return new Integer( x ); }          // Custom cast to "int":         public static implicit operator Int32( Integer x ) { return x.Value; }           public override string ToString()         {             return string.Format( "Integer({0})", Value );         }     } 
like image 36
MakePeaceGreatAgain Avatar answered Sep 18 '22 16:09

MakePeaceGreatAgain