Tools: 17. C# (Char)

Tools: 17. C# (Char)

Source: Dev.to

0. The Real Goal of This Lesson ## 1. string vs char — The Exact Difference ## string ## Core Difference ## 2. Why char Is More Accurate in Certain Situations ## 3. The Most Common Beginner Mistake ## Incorrect Code ## Correct Code ## 4. Full Runnable Example (Rider) ## 5. Experiments You Must Try ## Experiment 1 ## Experiment 2 ## 6. One More Important Concept ## A string is internally a sequence of char values ## 7. Is char Only for Letters? ## Final Summary “When you only need a single character, why should you use char instead of string?” This lesson is not about syntax. This is a modeling discipline step. A string can contain: This is not stylistic. Consider a grade conversion method: The return value is always: That is a single symbol. A more precise representation is: You are learning data modeling. It is a single symbolic value. char models that more precisely. This fails at compile time. Single quotes define a character literal. Run this entire program: The method returns a single character. You will get a compile-time error. Change the method signature to: Then return "A" instead of 'A'. Is the type modeling still precise? A string is a collection. A char is a single element inside that collection. Any single Unicode character is valid. char represents a single character. string represents a collection of characters. Choosing the more precise type leads to better data modeling and clearer intent. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK: string text = "A"; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: string text = "A"; CODE_BLOCK: string text = "A"; CODE_BLOCK: char letter = 'A'; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: char letter = 'A'; CODE_BLOCK: char letter = 'A'; CODE_BLOCK: static string ConvertPointsToGrade(int points) { return "A"; } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: static string ConvertPointsToGrade(int points) { return "A"; } CODE_BLOCK: static string ConvertPointsToGrade(int points) { return "A"; } CODE_BLOCK: "A", "B", "C" Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: "A", "B", "C" CODE_BLOCK: "A", "B", "C" CODE_BLOCK: static char ConvertPointsToGrade(int points) { return 'A'; } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: static char ConvertPointsToGrade(int points) { return 'A'; } CODE_BLOCK: static char ConvertPointsToGrade(int points) { return 'A'; } CODE_BLOCK: static char GetGrade() { return "A"; // compile-time error } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: static char GetGrade() { return "A"; // compile-time error } CODE_BLOCK: static char GetGrade() { return "A"; // compile-time error } CODE_BLOCK: static char GetGrade() { return 'A'; } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: static char GetGrade() { return 'A'; } CODE_BLOCK: static char GetGrade() { return 'A'; } CODE_BLOCK: using System; class Program { static void Main() { Console.WriteLine("Enter your score:"); string input = Console.ReadLine(); int score = int.Parse(input); char grade = ConvertToGrade(score); Console.WriteLine($"Your grade is: {grade}"); Console.ReadKey(); } static char ConvertToGrade(int score) { if (score >= 90) return 'A'; else if (score >= 80) return 'B'; else if (score >= 70) return 'C'; else if (score >= 60) return 'D'; else if (score >= 0) return 'F'; else return '!'; // invalid input indicator } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: using System; class Program { static void Main() { Console.WriteLine("Enter your score:"); string input = Console.ReadLine(); int score = int.Parse(input); char grade = ConvertToGrade(score); Console.WriteLine($"Your grade is: {grade}"); Console.ReadKey(); } static char ConvertToGrade(int score) { if (score >= 90) return 'A'; else if (score >= 80) return 'B'; else if (score >= 70) return 'C'; else if (score >= 60) return 'D'; else if (score >= 0) return 'F'; else return '!'; // invalid input indicator } } CODE_BLOCK: using System; class Program { static void Main() { Console.WriteLine("Enter your score:"); string input = Console.ReadLine(); int score = int.Parse(input); char grade = ConvertToGrade(score); Console.WriteLine($"Your grade is: {grade}"); Console.ReadKey(); } static char ConvertToGrade(int score) { if (score >= 90) return 'A'; else if (score >= 80) return 'B'; else if (score >= 70) return 'C'; else if (score >= 60) return 'D'; else if (score >= 0) return 'F'; else return '!'; // invalid input indicator } } CODE_BLOCK: return 'A'; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: return 'A'; CODE_BLOCK: return 'A'; CODE_BLOCK: return "A"; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: return "A"; CODE_BLOCK: return "A"; CODE_BLOCK: static string ConvertToGrade(int score) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: static string ConvertToGrade(int score) CODE_BLOCK: static string ConvertToGrade(int score) CODE_BLOCK: string name = "Sabin"; Console.WriteLine(name[0]); Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: string name = "Sabin"; Console.WriteLine(name[0]); CODE_BLOCK: string name = "Sabin"; Console.WriteLine(name[0]); CODE_BLOCK: using System; class Program { static void Main() { string word = "Hello"; char firstLetter = word[0]; Console.WriteLine(firstLetter); Console.ReadKey(); } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: using System; class Program { static void Main() { string word = "Hello"; char firstLetter = word[0]; Console.WriteLine(firstLetter); Console.ReadKey(); } } CODE_BLOCK: using System; class Program { static void Main() { string word = "Hello"; char firstLetter = word[0]; Console.WriteLine(firstLetter); Console.ReadKey(); } } CODE_BLOCK: H Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: char symbol = '!'; char numberChar = '5'; char space = ' '; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: char symbol = '!'; char numberChar = '5'; char space = ' '; CODE_BLOCK: char symbol = '!'; char numberChar = '5'; char space = ' '; - Representing data precisely - Developing sensitivity to type modeling - Choosing the smallest accurate abstraction - A sequence of characters - Uses double quotes " " - Internally represents multiple char values - One character - Many characters - Even zero characters - Exactly one character - Uses single quotes ' ' - Represents a single Unicode value - Not a sentence - Not multiple characters - "A" is a string - The method expects a char - These are different types