What is the difference between an int and an Integer in Java and C#?

前端 未结 26 1471
生来不讨喜
生来不讨喜 2020-11-22 12:00

I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an i

26条回答
  •  有刺的猬
    2020-11-22 12:40

    This has already been answered for Java, here's the C# answer:

    "Integer" is not a valid type name in C# and "int" is just an alias for System.Int32. Also, unlike in Java (or C++) there aren't any special primitive types in C#, every instance of a type in C# (including int) is an object. Here's some demonstrative code:

    void DoStuff()
    {
        System.Console.WriteLine( SomeMethod((int)5) );
        System.Console.WriteLine( GetTypeName() );
    }
    
    string SomeMethod(object someParameter)
    {
        return string.Format("Some text {0}", someParameter.ToString());
    }
    
    string GetTypeName()
    {
        return (typeof (T)).FullName;
    }
    

提交回复
热议问题