Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
string str = "abcd"; 
for(int i=0;i<str.length;++i)>
console.write(str[i]); 

output: abcd

it seems like str is actually an array of characters!
this is being confusing!

we didn't declare any arrays, but how come we have str[0]=a and so on...?!

[Agent_Spock]
- Added code brackets
Posted
Updated 22-Jun-14 23:15pm
v2
Comments
[no name] 22-Jun-14 18:52pm    
http://stackoverflow.com/questions/3669199/c-sharp-is-string-actually-an-array-of-chars-or-does-it-just-have-an-indexer
agent_kruger 23-Jun-14 5:15am    
please explain in detail what you want sir?

If you only interested in the way how C# (and more specifically the .NET framework) looks at String - it is NOT an array.
The string[index] works because String class has an indexer to implement it...
C#
public extern char this[int index]
{
}
 
Share this answer
 
It's both at the same time. An array is nothing but a pointer to a block of memory offset from a base address.

A string is a contiguous block of memory starting at the same address containing what is considered characters. These can be either one or more bytes long for each character. The pointer size matches the size of the characters in bytes.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 23-Jun-14 5:48am    
It's right only at the memory level, but .NET and in it C# does not see String as an array-of-char!
Array is nothing but the collection of similar type of data . And a string is a block of memory which contains chars in contentious manner. In simple words a string is also known as a char array .

So in the above case you have got that output .
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 23-Jun-14 5:48am    
It's right only at the memory level, but .NET and in it C# does not see String as an array-of-char!

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