Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
hi there

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



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


but it show the output

C#
system.int32[]


anyone know why?
Posted
Comments
Sergey Alexandrovich Kryukov 24-Apr-15 3:25am    
Exactly. Anyone knows why. Almost anyone.
If you don't, don't worry, I'll explain it to you.
—SA

Because
Console.WriteLine(samp);
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());
    }
}
 
Share this answer
 
v2
Comments
TheRealSteveJudge 24-Apr-15 3:33am    
5*
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.
 
Share this answer
 
Comments
TheRealSteveJudge 24-Apr-15 3:33am    
5*
Abhinav S 24-Apr-15 3:43am    
Thank you.
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,
C#
Console.WriteLine(samp[0]);
   // will show 100, because 100.ToString() == "100";
Console.WriteLine(samp[1]); 
   // will show 100, because 200.ToString() == "200";


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
 
Share this answer
 
v2
try following:-


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


C#
foreach(int a in samp)
{
Console.WriteLine(a);
}
Console.ReadyKey();
 
Share this answer
 
If you want all the values present in array then you have to write a for loop then inside that for loop you can print their values.However console.WriteLine(Sample)means you are trying to print the datatype info of that array.because array is not a variable.
 
Share this answer
 
int[] samp = new int[2];
samp[0] = 100;
samp[1] = 200;
Console.WriteLine("the results are {0} and {1}",samp[0], samp[1]);
Console.ReadKey();

or

int[] samp = new int[2];
samp[0] = 100;
samp[1] = 200;
Console.WriteLine(samp[0]);
Console.WriteLine(samp[1]);
Console.ReadKey();
or
int[] samp = new int[2];
samp[0] = 100;
samp[1] = 200;
for (int i = 0; i < 2; i++)
{
Console.WriteLine(samp[i]);
}
Console.ReadKey();

because samp is a set of numbers(array of numbers). Inorder to see each number we need to specify the order - samp[number](eg: samp[0])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900