Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
char vs string keywords in C#
The char and string keywords in C# are used to work with textual data, but they serve different purposes. The char keyword represents a single character, while string represents a sequence of characters. Understanding the difference between these two data types is essential for effective text manipulation in C#.
Syntax
Following is the syntax for declaring a char variable −
char variableName = 'A'; // Single quotes for character
Following is the syntax for declaring a string variable −
string variableName = "Hello"; // Double quotes for string
char Keyword
The char keyword represents a single Unicode character and is an alias for System.Char. It stores exactly one character and uses single quotes for character literals.
Example
using System;
class Program {
public static void Main() {
char letter = 'A';
char digit = '9';
char symbol = '@';
Console.WriteLine("Letter: " + letter);
Console.WriteLine("Digit: " + digit);
Console.WriteLine("Symbol: " + symbol);
// Array of characters
char[] letters = { 'H', 'e', 'l', 'l', 'o' };
Console.WriteLine("Character array: " + new string(letters));
}
}
The output of the above code is −
Letter: A Digit: 9 Symbol: @ Character array: Hello
string Keyword
The string keyword represents a sequence of characters and is an alias for System.String. Strings are immutable in C#, meaning their values cannot be changed after creation. String literals use double quotes.
Example
using System;
class Program {
public static void Main() {
string name = "Tom Hanks";
string message = "Hello World";
Console.WriteLine("Name: " + name);
Console.WriteLine("Message: " + message);
Console.WriteLine("Length of name: " + name.Length);
// Array of strings
string[] greetings = { "Hello", "From", "Tutorials", "Point" };
Console.WriteLine("String array:");
foreach(string greeting in greetings) {
Console.WriteLine(greeting);
}
}
}
The output of the above code is −
Name: Tom Hanks Message: Hello World Length of name: 9 String array: Hello From Tutorials Point
Converting Between char and string
You can convert between char and string types using various methods −
Example
using System;
class Program {
public static void Main() {
char ch = 'C';
string str = "Sharp";
// Convert char to string
string charToString = ch.ToString();
Console.WriteLine("Char to String: " + charToString);
// Get first character from string
char firstChar = str[0];
Console.WriteLine("First character: " + firstChar);
// Convert string to char array
char[] charArray = str.ToCharArray();
Console.WriteLine("String to char array:");
foreach(char c in charArray) {
Console.WriteLine(c);
}
}
}
The output of the above code is −
Char to String: C First character: S String to char array: S h a r p
Comparison
| char | string |
|---|---|
| Stores a single character | Stores a sequence of characters |
| Uses single quotes: 'A' | Uses double quotes: "Hello" |
| Value type (2 bytes) | Reference type (variable size) |
| Mutable when part of array | Immutable |
| Alias for System.Char | Alias for System.String |
Conclusion
The char keyword is used for single characters with single quotes, while string is used for text sequences with double quotes. Understanding their differences helps in choosing the appropriate data type for text manipulation tasks in C# programming.
