Can anybody explain me why this is happening?
class Program
{
static char[] ch = new char[2];
static string name = "Ivankata";
static void Main(string[] args)
{
inputChar();
}
static void inputChar()
{
ch = name.ToCharArray();
Console.WriteLine(ch);
}
}
My char array named ch accepts only 2 chars however when I convert my string "Ivankata" to char array it works somehow? Shouldn't it cut the remaining "ankata" and show only "iv"? Can anybody explain what's going on here?
string.ToCharArray()creates a new char array iternally and you store the reference to new array inch. The old array with a length of two is not referenced anymore and will be collected by the Garbage Collector soon.