Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue in printing Char Array() which is converted from String

Got stuck with this.. can you please explain what is happening in it? or give me any link!

String s1="C# Example";
Char[] s3 = s1.ToCharArray();

Console.WriteLine("S3 : {0}",s3);

I want to display the Character which is converted. Output displayed is System.Char[]. Now i need to do some changes, but what is that ?

It is possible in two ways.

1) I need to Change it to String, before i'm going to Print.

Or

2) I need to print it with Char by defining the index, (i.e) s3[0];

Am i correct. Anything More?

like image 448
Balaji Avatar asked Jul 04 '26 17:07

Balaji


1 Answers

The explanation of what happens:

  • Console.WriteLine("{0}", s3) calls s3.ToString().
    • Because WriteLine() calls ToString() on each argument
  • Method ToString() isn't overridden in type System.Array so Object.ToString() is called.
    • Because Char[] is System.Array and all types inherit from Systen.Object.
  • Which is equivalent to s3.GetType().ToString() and outputs System.Char[].
    • Because this is the default implementation. Subtypes can override it. For instance, System.String does, StringBuilder too.
like image 108
abatishchev Avatar answered Jul 07 '26 05:07

abatishchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!