Files
2nd/10_Wiki/Dev/Topic_CSharp/CSharp_Polymorphism.md
T
Antigravity Agent 1cfd3bbb56 docs(10_Wiki): 위키 구조 정리 — 언어 튜토리얼 카테고리 폴더 제거 + 신규 자산 동기화
Topic_CSS/Topic_HTML/Topic_JavaScript/Topic_Prompt/Topic_Comfyui 등 기존 카테고리 폴더를 정리하고,
Topic_Graphic/Dev 등 신규 산출물과 Topics 내부 세션/메모리 기록을 동기화.
2026-07-05 00:10:59 +09:00

5.6 KiB

id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
id title category status verification_status canonical_id aliases duplicate_of source_trust_level confidence_score created_at updated_at review_reason merge_history tags raw_sources applied_in github_commit
csharp-polymorphism C# Polymorphism Programming_Language draft conceptual
virtual override C#
method overriding C#
C# 다형성
B 0.85 2026-07-04 2026-07-04
csharp
programming-language
w3schools
oop
polymorphism
https://www.w3schools.com/cs/cs_polymorphism.php

CSharp Polymorphism

🎯 한 줄 통찰 (One-line insight)

C# requires the EXACT same two-keyword pairing already confirmed for modern C++ in Topic_CPP — virtual on the base class method, override on each derived class's replacement — and this chapter goes further than the CPP chapter did by explicitly demonstrating the FAILURE case first: without virtual/override, calling animalSound() through an Animal-typed reference to a Pig/Dog object prints the BASE class's message every time, because (per the tutorial's own explanation) same-named methods without these keywords simply aren't polymorphic at all — they're just separately-named methods that happen to share an identifier. [S1]

🧠 핵심 개념 (Core concepts)

  • Polymorphism ("many forms") — occurs among classes related by inheritance; lets a single action be performed in different ways depending on the actual object type. [S1]
  • Without virtual/override — same-named methods in base and derived classes do NOT achieve polymorphism; calling the method through a base-typed reference always runs the BASE class's version, regardless of the actual object's derived type. [S1]
  • virtual keyword — placed on the BASE class method to allow it to be overridden. [S1]
  • override keyword — placed on each DERIVED class's replacement method, required alongside base virtual to achieve true polymorphic dispatch. [S1]
  • Base-typed reference, derived objectAnimal myPig = new Pig(); — the variable's declared TYPE is the base class, but the actual OBJECT is the derived class; with virtual/override, the call resolves to the derived class's implementation at runtime. [S1]

📖 세부 내용 (Details)

  • Without virtual/override (the "not the output I was looking for" case): base and derived Animal/Pig/Dog each define their own animalSound(), but Animal myPig = new Pig(); myPig.animalSound(); prints "The animal makes a sound" for ALL three objects, not the derived versions. [S1]
  • With virtual/override: public virtual void animalSound() in Animal, public override void animalSound() in Pig and DogmyAnimal.animalSound() prints "The animal makes a sound", myPig.animalSound() prints "The pig says: wee wee", myDog.animalSound() prints "The dog says: bow wow" — each resolves to its OWN class's implementation. [S1]

⚖️ 모순 및 업데이트 (Contradictions & updates)

  • virtual/override 페어링이 C++와 완전히 동일: 기반 클래스에 virtual, 파생 클래스에 override를 붙여야 다형성이 작동한다는 규칙이 Topic_CPP의 Virtual Functions 챕터(C++11의 override 키워드 포함)와 차이가 없다는 점이 확인됨. [S1]
  • 실패 사례를 먼저 보여줌으로써 virtual의 필요성을 명확히 함: Topic_CPP의 Virtual Functions 챕터와 달리, 이번 챕터는 virtual/override 없이 먼저 실행해 "기대와 다른 출력"이 나오는 것을 보여준 뒤에야 virtual/override를 도입한다는 점이 교육적 구성의 차이로 확인됨. [S1]

🛠️ 적용 사례 (Applied in summary)

Animal 기반 클래스와 Pig/Dog 파생 클래스가 각각 다른 animalSound()를 구현하고, virtual/override 없이는 모두 기반 클래스 메시지만 출력되다가 virtual/override를 추가하면 각 동물 고유의 소리가 출력되는 before/after 비교가 원문에서 직접 실전 활용 사례로 제시됨. [S1]

💻 코드 패턴 (Code patterns)

Without vs. with virtual/override — same call site, different resolved method (C#):

// Without virtual/override -- always prints base class message
class Animal { public void animalSound() { Console.WriteLine("The animal makes a sound"); } }
class Pig : Animal { public void animalSound() { Console.WriteLine("The pig says: wee wee"); } }
Animal myPig1 = new Pig();
myPig1.animalSound(); // "The animal makes a sound" -- NOT polymorphic

// With virtual/override -- resolves to the actual object's type
class Animal2 { public virtual void animalSound() { Console.WriteLine("The animal makes a sound"); } }
class Pig2 : Animal2 { public override void animalSound() { Console.WriteLine("The pig says: wee wee"); } }
Animal2 myPig2 = new Pig2();
myPig2.animalSound(); // "The pig says: wee wee" -- polymorphic

검증 상태 및 신뢰도

  • 상태: draft
  • 검증 단계: conceptual
  • 출처 신뢰도: B (W3Schools — widely used educational reference, not a primary standards body)
  • 신뢰 점수: 0.85
  • 중복 검사 결과: 신규 생성 (New discovery)

🔗 지식 그래프 (Knowledge Graph)

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-07-04: Initial draft synthesized from the W3Schools "C# Polymorphism" page (Astra wiki-curation, P-Reinforce v3.1 format).