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
.
int is a type, not 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 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.
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 .
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
.
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 ); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With