c#中简单的控制台应用 [英] simple console application in c#

查看:67
本文介绍了c#中简单的控制台应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi there

i am new in C#, i try to this console application










int[] samp=new int[2];
           samp[0] = 100;
           samp[1] = 200;
           Console.WriteLine(samp);
           Console.ReadKey();





但显示输出





but it show the output

system.int32[]





任何人都知道为什么?



anyone know why?

推荐答案

因为
Console.WriteLine(samp);

调用ToString(),它获取表示对象的字符串,这是一个int32数组。



要获取数组中的每个值,您需要枚举数组。

invokes ToString() which gets the string representing the object, which is an int32 array.

To get each value within the array, you need to enumerate over the array.

if(samp != null) // Unneccesary here, but general good practise to avoid exceptions
{
    foreach(var item in samp)
    {
        Console.WriteLine(item.ToString());
    }
}


变量 samp 被定义为数组。

如果你直接打印一个数组( Console.WriteLine(samp); ),你会得到你得到的消息,即system.int32 [] 。



要打印整数值,您需要使用例如索引 Console.WriteLine(samp [0]);



或者,您可以在数组上运行循环打印所有的值。
The variable samp is defined as an array.
If you print an array directly(Console.WriteLine(samp);), you will get the message you get i.e. system.int32[].

To print the integer values, you need to use the index for e.g. Console.WriteLine(samp[0]);.

Alternately, you can run a loop over the array and print all the values.


很难说你不明白,可能很少。



第一件事要了解的是:打字系统。所有对象都派生自 System.Object 。值对象(例如基本类型的对象)通过装箱实现 System.Object 成员。



事情是:有许多 Console.WriteLine 方法使用任何类型的参数,包括通过 param <的任意数量的参数/ code>说明符。这是他们基本上做的:对于每个参数,该方法调用其重写的 System.Object.ToString()方法。



例如,

It's hard to say what you don't understand, probably few things.

First thing to understand is: typing system. All objects are derived from System.Object. Value objects, such as the object of primitive types, implement System.Object members through boxing.

The thing is: there are many Console.WriteLine methods working with any kinds of parameters, including arbitrary number of parameters via param specifier. Here is what they essentially do: for each parameter, the method calls its overridden System.Object.ToString() method.

For example,
Console.WriteLine(samp[0]);
   // will show 100, because 100.ToString() == "100";
Console.WriteLine(samp[1]); 
   // will show 100, because 200.ToString() == "200";





但你传递 samp ,这是一个引用类型的对象,阵列。对于此类对象,默认 System.Object.ToString() return只是对象类型。实际上,没有任何明显的预定义方式来显示任意对象的内容。当您定义自己的类或结构类型时,您始终可以覆盖此实例方法,使用任何实例成员和任何计算来返回一些字符串,类型为 System.String 。



-SA



But you pass samp, which is a reference-type object, an array. For such objects, default System.Object.ToString() return is just the object type. Indeed, there is no any obvious predefined way to show content of an arbitrary object. When you define your own class or structure types, you can always override this instance method, using any instance members and any calculations to return some string, the object of the type System.String.

—SA


这篇关于c#中简单的控制台应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆