java c# 中是否有 Integer 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30296734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:50:45  来源:igfitidea点击:

Is there a Integer class in c#?

javac#

提问by Abhishek

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

我们有IntegerJAVA 类,但我在 C# 中找不到任何等效的类?c# 有什么等价物吗?如果没有,我如何Integer在 C# 中获得 JAVA类行为?

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 Integerand I don't think I can create reference of intor Int32.

这是因为我正在尝试将 JAVA 代码迁移到 c# 代码。如果有等效的方法,那么代码迁移会更容易。另外,我需要存储 的引用,Integer我认为我无法创建int或 的引用Int32

回答by recursive

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

C# 具有统一的类型系统,因此int可以隐式装箱到object引用中。IntegerJava 存在的唯一原因是它可以转换为对象引用并存储在引用中以供其他容器类使用。

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

由于 C# 可以在没有其他类型的情况下做到这一点,因此没有对应的Integer.

回答by HimBromBeere

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 Integerthat 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 Int32or its alias int. However you may of course write your own wrapper-class:

如果没有任何手动更改,代码迁移对于任何类型的语言都不会开箱即用。有些东西例如Integer根本不存在于其中的类(C# 为什么要这样做,请参阅递归答案),因此您必须自己做一些工作。与您所追求的最接近的等价物是Int32或其别名int。但是,您当然可以编写自己的包装类:

    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 );
        }
    }

回答by PC Luddite

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 Integerclass is required to wrap the intprimitive. In C# intis synonym for the Int32struct.

C# 的美妙之处在于它具有统一的类型系统。一切都源自object,甚至是原始类型。因此,所有关键字都只是相应类或结构的别名。Java 不使用统一的类型系统,因此需要一个单独的Integer类来包装int原语。在 C# 中intInt32结构的同义词。

What you're looking for has been right in front of you the whole time. Start using the dot notation directly on the intkeyword (i.e. int.whatever()) to access the all goodness of the .NET version of the Javian Integerclass.

您正在寻找的东西一直就在您的面前。开始直接在int关键字(即int.whatever())上使用点符号来访问 JavianInteger类的 .NET 版本的所有优点。

回答by Theo

I did some testing with Nullable types in a console application and it appears that they do not behave as you wish. For example:

我在控制台应用程序中对 Nullable 类型进行了一些测试,但它们的行为似乎并不如您所愿。例如:

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 Main
    Console.WriteLine(l[0]);//Writes 3
    Console.WriteLine(y);//writes 2
    Foo2(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 Main
    Console.WriteLine(l[0]);//Writes 3
    Console.WriteLine(y);//writes 3
    Foo2(l);
}

private static void Foo2(List<MyType<int>> l)
{
    l[0].Value += 1;
    Console.WriteLine(l[0]);//writes 4
}

回答by Mainak Saha

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.Int32 都是结构体,因此都是值类型,无法与 Java 的 Integer 包装器类相比,后者是一种引用类型。

System.Object class though a reference type can cause issue as boxing creates immutable object. In short, you can't alter a boxed value.

System.Object 类虽然引用类型可能会导致问题,因为装箱会创建不可变对象。简而言之,您无法更改装箱值。

        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.

与 Java 的 Integer 完全对应的是自定义的通用包装器类。

        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 to
        Console.WriteLine($"wrapA = {wrapA}, wrapB={wrapB}");
        //Output: wrapA = 40, wrapB=40
        Console.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;
}

回答by mimi87

c# have a integer type called int link is here

c# 有一个整数类型叫做 int link is here

https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx

https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx