--- id: csharp-user-input title: "C# User Input" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Console.ReadLine", "C# 사용자 입력"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "user-input"] raw_sources: ["https://www.w3schools.com/cs/cs_user_input.php"] applied_in: [] github_commit: "" --- # [[CSharp User Input]] ## 🎯 한 줄 통찰 (One-line insight) This chapter is the exact payoff the Type Casting chapter promised — "take a look at the next chapter, when working with user input, to see an example of this [conversion]" — and it delivers precisely that: `Console.ReadLine()` ALWAYS returns a `string`, full stop, unlike C++'s `cin >>` or C's `scanf()` which can read directly into a typed variable (`int`, `double`, etc.); trying `int age = Console.ReadLine();` is a compile error ("Cannot implicitly convert type 'string' to 'int'"), forcing every numeric input in C# through an explicit `Convert.ToInt32()` (or similar) call, with a runtime `System.FormatException` risk if the user types non-numeric text — a failure mode C's `scanf()` doesn't raise as an exception (it silently fails to parse) and C++'s `cin >>` handles via the stream-error-state mechanism already documented in `[[CPP Input Validation]]`. [S1] ## 🧠 핵심 개념 (Core concepts) - **`Console.ReadLine()`** — reads a line of keyboard input and returns it as a `string`; the counterpart to `Console.WriteLine()`. [S1] - **Always returns `string`** — there is no direct "read an int" or "read a double" variant; every input arrives as text first. [S1] - **Implicit conversion is illegal** — `int age = Console.ReadLine();` fails at compile time with "Cannot implicitly convert type 'string' to 'int'". [S1] - **`Convert.ToInt32()` (and siblings)** — the explicit conversion method required to turn the string input into a usable numeric type, directly reusing the `Convert.To*` family from the Type Casting chapter. [S1] - **Runtime parse failure** — if the user enters non-numeric text where a number is expected, `Convert.ToInt32()` throws a `System.FormatException` ("Input string was not in a correct format.") — a RUNTIME exception, not a compile error; full handling deferred to a later Exceptions chapter. [S1] ## 📖 세부 내용 (Details) - Basic string input: `Console.WriteLine("Enter username:"); string userName = Console.ReadLine(); Console.WriteLine("Username is: " + userName);`. [S1] - Failing numeric input attempt: `Console.WriteLine("Enter your age:"); int age = Console.ReadLine(); Console.WriteLine("Your age is: " + age);` → compile error. [S1] - Fixed with explicit conversion: `Console.WriteLine("Enter your age:"); int age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Your age is: " + age);`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **Type Casting 챕터의 예고가 이 챕터에서 실현됨**: Type Casting 챕터가 "다음 챕터(사용자 입력)에서 왜 변환이 필요한지 확인하라"고 예고했는데, 이번 챕터가 정확히 그 이유(ReadLine()이 항상 string만 반환)와 해결책(Convert.ToInt32())을 보여준다는 점이 확인됨. [S1] - **C/C++의 typed 입력과 달리 항상 string으로만 들어옴**: C의 scanf()나 C++의 cin >>는 변수 타입에 맞춰 직접 파싱해 읽을 수 있었지만, C#의 Console.ReadLine()은 항상 string만 반환하므로 숫자가 필요하면 반드시 Convert.ToInt32() 같은 명시적 변환을 거쳐야 한다는 점이 확인됨 — 컴파일 타임 에러(암묵적 변환 금지)로 이를 강제함. [S1] ## 🛠️ 적용 사례 (Applied in summary) 사용자로부터 나이를 입력받아 Convert.ToInt32(Console.ReadLine())으로 정수로 변환한 뒤 출력하는 예제가 원문에서 직접 실전 활용 사례로 제시됨. [S1] ## 💻 코드 패턴 (Code patterns) Console.ReadLine() always returns string — explicit conversion required for numbers (C#): ```csharp Console.WriteLine("Enter your age:"); int age = Convert.ToInt32(Console.ReadLine()); // explicit conversion required Console.WriteLine("Your age is: " + age); // Non-numeric input throws System.FormatException at runtime ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.85 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[C# Tutorial]] - **관련 개념:** [[CSharp Type Casting]], [[CSharp Enums]], [[CSharp Exceptions]], [[CPP Input Validation]] - **참조 맥락:** User Input 섹션의 유일 챕터 — Exceptions 섹션으로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — C# User Input — https://www.w3schools.com/cs/cs_user_input.php ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# User Input" page (Astra wiki-curation, P-Reinforce v3.1 format).