find the first character in a string in csharp

To find the first character in a string in C#, you can use indexing to access the character at position 0. Here's an example:

main.cs
string myString = "hello world";
char firstChar = myString[0];
Console.WriteLine(firstChar); // Output: 'h'
108 chars
4 lines

In this example, the myString[0] expression gets the character at index 0 (the first character) in the string. This character is then assigned to the firstChar variable and printed to the console.

Note that if the string is empty (i.e. has a length of 0), trying to index it will result in an IndexOutOfRangeException. It's therefore a good idea to check the length of the string before trying to index it.

gistlibby LogSnag