--- id: csharp-properties title: "C# Properties (Get and Set)" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["get set C#", "automatic properties C#", "encapsulation C#", "C# ํ”„๋กœํผํ‹ฐ"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "oop", "properties", "encapsulation"] raw_sources: ["https://www.w3schools.com/cs/cs_properties.php"] applied_in: [] github_commit: "" --- # [[CSharp Properties]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) This is the chapter where the `.Length`-is-a-property-not-a-method observation from way back in the Strings chapter finally gets explained: a C# property is a LANGUAGE-LEVEL construct combining a private field with `get`/`set` accessor blocks, and โ€” unlike C++, which has no native property syntax and requires programmers to write plain `getName()`/`setName()` METHODS by convention โ€” C# additionally offers "automatic properties" (`public string Name { get; set; }`) that need no backing field written at all, making encapsulation nearly free syntactically where C++ makes it entirely manual boilerplate. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **Encapsulation** โ€” hiding sensitive data by declaring fields `private`, then exposing controlled access through `public` get/set methods via properties. [S1] - **Property** โ€” a hybrid of a variable and a method, containing a `get` accessor and a `set` accessor. [S1] - **`get` accessor** โ€” returns the value of the associated private field. [S1] - **`set` accessor** โ€” assigns a value to the associated private field; the special `value` keyword represents whatever value is being assigned to the property. [S1] - **Naming convention** โ€” the property should share the field's name but start with an UPPERCASE letter (`name` field โ†” `Name` property). [S1] - **Automatic (shorthand) properties** โ€” `public string Name { get; set; }` โ€” no backing field declared manually; the compiler generates one implicitly, producing identical behavior with less code. [S1] - **Encapsulation benefits** โ€” better control over class members, ability to make a field effectively read-only (get-only) or write-only (set-only), flexibility to change internals without breaking external code, and increased data security. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Full manual property: `class Person { private string name; public string Name { get { return name; } set { name = value; } } }`. [S1] - Usage: `Person myObj = new Person(); myObj.Name = "Liam"; Console.WriteLine(myObj.Name);` โ†’ `"Liam"` โ€” accessed via the SAME dot syntax as a plain field, even though a get/set pair runs underneath. [S1] - Automatic property equivalent: `class Person { public string Name { get; set; } }` โ€” same usage, same output, but no explicit `name` field or accessor bodies written. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **Strings ์ฑ•ํ„ฐ์˜ .Length ํ”„๋กœํผํ‹ฐ ๋ฏธ์Šคํ„ฐ๋ฆฌ๊ฐ€ ์—ฌ๊ธฐ์„œ ํ’€๋ฆผ**: Topic_CSharp์˜ Strings ์ฑ•ํ„ฐ์—์„œ `.Length`๊ฐ€ ๊ด„ํ˜ธ ์—†๋Š” ํ”„๋กœํผํ‹ฐ๋ผ๊ณ  ํ™•์ธํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ ์ฑ•ํ„ฐ๊ฐ€ ํ”„๋กœํผํ‹ฐ์˜ ์‹ค์ œ ์ •์˜(private ํ•„๋“œ + get/set)๋ฅผ ์„ค๋ช…ํ•จ์œผ๋กœ์จ ๊ทธ ๋ฌธ๋ฒ•์  ์ •์ฒด๊ฐ€ ๋ช…ํ™•ํ•ด์ง. [S1] - **C++์—๋Š” ์—†๋Š” ์–ธ์–ด ์ฐจ์›์˜ ํ”„๋กœํผํ‹ฐ ๋ฌธ๋ฒ•**: C++๋Š” ํ”„๋กœํผํ‹ฐ๋ผ๋Š” ์–ธ์–ด ๊ธฐ๋Šฅ์ด ์—†์–ด getName()/setName() ๊ฐ™์€ ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ด€๋ก€์ ์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ์ง€๋งŒ, C#์€ get/set ์ ‘๊ทผ์ž๋ฅผ ์–ธ์–ด ๋ฌธ๋ฒ•์œผ๋กœ ์ œ๊ณตํ•˜๋ฉฐ ์‹ฌ์ง€์–ด ์ž๋™ ํ”„๋กœํผํ‹ฐ(`{ get; set; }`)๋กœ ๋ฐฑํ‚น ํ•„๋“œ์กฐ์ฐจ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ํ™•์ธ๋จ โ€” ์บก์Аํ™” ๊ตฌํ˜„ ๋น„์šฉ์ด C++๋ณด๋‹ค ํ›จ์”ฌ ๋‚ฎ์Œ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) Person ํด๋ž˜์Šค์˜ private name ํ•„๋“œ๋ฅผ public Name ํ”„๋กœํผํ‹ฐ(get/set)๋กœ ๊ฐ์‹ธ ์™ธ๋ถ€์—์„œ myObj.Name์œผ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ์ฝ๊ณ  ์“ฐ๋Š” ์˜ˆ์ œ, ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์ž๋™ ํ”„๋กœํผํ‹ฐ๋กœ ์ถ•์•ฝํ•œ ๋™์ผ ๊ฒฐ๊ณผ์˜ ์˜ˆ์ œ๊ฐ€ ์›๋ฌธ์—์„œ ์ง์ ‘ ์‹ค์ „ ํ™œ์šฉ ์‚ฌ๋ก€๋กœ ์ œ์‹œ๋จ. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) Manual property vs. automatic (shorthand) property โ€” same behavior (C#): ```csharp // Manual class Person { private string name; public string Name { get { return name; } set { name = value; } } } // Automatic (shorthand) -- no backing field written class PersonShort { public string Name { get; set; } } ``` ## โœ… ๊ฒ€์ฆ ์ƒํƒœ ๋ฐ ์‹ ๋ขฐ๋„ - **์ƒํƒœ:** draft - **๊ฒ€์ฆ ๋‹จ๊ณ„:** conceptual - **์ถœ์ฒ˜ ์‹ ๋ขฐ๋„:** B (W3Schools โ€” widely used educational reference, not a primary standards body) - **์‹ ๋ขฐ ์ ์ˆ˜:** 0.86 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[C# Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[CSharp Access Modifiers]], [[CSharp Strings]], [[CSharp Inheritance]], [[CPP Encapsulation]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** Access Modifiers & Properties ์„น์…˜์˜ ๋งˆ์ง€๋ง‰ ์ฑ•ํ„ฐ โ€” Inheritance ์„น์…˜์œผ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” C# Properties (Get and Set) โ€” https://www.w3schools.com/cs/cs_properties.php ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Properties (Get and Set)" page (Astra wiki-curation, P-Reinforce v3.1 format).