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
.
Best Answer
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 );}}
The beauty of C# is that it has a unified type system. Everything derives from object
, even primitive types. Because of this, all keywords are simply aliases for a corresponding class or struct. Java does not use a unified type system, so a separate Integer
class is required to wrap the int
primitive. In C# int
is synonym for the Int32
struct.
What you're looking for has been right in front of you the whole time. Start using the dot notation directly on the int
keyword (i.e. int.whatever()
) to access the all goodness of the .NET version of the Javian Integer
class.
I did some testing with Nullable types in a console application and it appears that they do not behave as you wish. For example:
static void Main(string[] args){int? x = 1;Foo(ref x);Console.WriteLine(x);//Writes 2}private static void Foo(ref int? y){y += 1;var l = new List<int?>();l.Add(y);l[0] += 1;//This does not affect the value of x devlared in MainConsole.WriteLine(l[0]);//Writes 3Console.WriteLine(y);//writes 2Foo2(l);}private static void Foo2(List<int?> l){l[0] += 1;Console.WriteLine(l[0]);//writes 4}
But if you roll your own generic class to wrap primitive/value types for use within your application you can get the behavior you are expecting:
public class MyType<T>{public T Value { get; set; }public MyType() : this(default(T)){}public MyType(T val){Value = val;}public override string ToString(){return this.Value.ToString();}}static void Main(string[] args){var x = new MyType<int>(1);Foo(x);Console.WriteLine(x);//Writes 4}private static void Foo(MyType<int> y){y.Value += 1;var l = new List<MyType<int>>();l.Add(y);l[0].Value += 1;//This does affect the value of x devlared in MainConsole.WriteLine(l[0]);//Writes 3Console.WriteLine(y);//writes 3Foo2(l);}private static void Foo2(List<MyType<int>> l){l[0].Value += 1;Console.WriteLine(l[0]);//writes 4}
int, int? and System.Int32 are all struct and thus value types and does not compare to Java's Integer wrapper class which is a reference type.
System.Object class though a reference type can cause issue as boxing creates immutable object. In short, you can't alter a boxed value.
int a = 20;Object objA = a; //Boxes a value type into a reference type, objA now points to boxed [20]Object objB = objA; //Both objA & objB points to boxed [20]objA = 40; //While objB points to boxed [20], objA points to a new boxed [40]//Thus, it creates another ref type boxing a 40 value integer value type, //Boxed values are immutable like string and above code does not alter value of previous boxed value [20]Console.WriteLine($"objA = {objA}, objB={objB}");//Output: objA = 40, objB=20
What exactly corresponds to Java's Integer is a custom generic wrapper class.
int a = 20;Wrapper<int> wrapA = new Wrapper<int>(a);Wrapper<int> wrapB = wrapA; //both wrapA and wrapB are pointing to [20]wrapA.Value = 40; //Changing actual value which both wrapA and wrapB are pointing toConsole.WriteLine($"wrapA = {wrapA}, wrapB={wrapB}");//Output: wrapA = 40, wrapB=40Console.ReadKey();
Implementation of the wrapper class is given below:
public class Wrapper<T> where T : struct{public static implicit operator T(Wrapper<T> w){return w.Value;}public Wrapper(T t){_t = t;}public T Value{get{return _t;}set{_t = value;}}public override string ToString(){return _t.ToString();}private T _t;}
As pointed out in other answers, C# has a unified type system so everything derives from object
. If you need to handle null
values then use int?
to specify that the integer object can be null
.
c# have a integer type called int link is here
https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx