π» Programming Language/C#
μ€ν°λ) λλ²μ§Έ μ€ν°λ νΌλλ°± λ° μ§λ¬Έ μ 리λ΄μ©
S.Honey
2022. 4. 8. 09:21
sealed
ν€μλλ₯Ό λ©μλμ μ¬μ©!
- λΆλͺ¨ν΄λμ€μμ μ€μλμ νκ²λ νλ μμμ μμν΄λμ€μμ 건λ리면 μλλ κ²½μ°
- λ©μλμ
sealed
νμ μλ₯Ό μ¬μ©ν΄μ μ νν μ μλ€.
- λ©μλμ
class Parent
{
public int num;
public int count;
public Parent(int num)
{
this.num = num;
this.count = 0;
Console.WriteLine("Parent μμ±μ");
}
public virtual void addCount() { }
public void showInfo()
{
Console.WriteLine("Num : {0}, Count : {1}", this.num, this.count);
}
}
class Counter : Parent
{
public Counter(int num) : base(num)
{
Console.WriteLine("Counter μμ±μ");
}
public sealed override void addCount()
{
base.count++;
}
}
class Calculator : Counter
{
public Calculator(int num) : base(num)
{
Console.WriteLine("Calculator μμ±μ");
}
public void add()
{
Console.WriteLine("λνκΈ° 1");
this.num++;
base.addCount();
}
public void sub()
{
Console.WriteLine("λΉΌκΈ° 1");
this.num--;
base.addCount();
}
}
class Program
{
static void Main(string[] args)
{
Parent ncc = new Calculator(10);
ncc.showInfo();
Calculator c = (Calculator)ncc;
c.add();
c.add();
c.sub();
c.sub();
c.sub();
c.showInfo();
}
}
Output
Parent μμ±μ
Counter μμ±μ
Calculator μμ±μ
Num : 10, Count : 0
λνκΈ° 1
λνκΈ° 1
λΉΌκΈ° 1
λΉΌκΈ° 1
λΉΌκΈ° 1
Num : 9, Count : 5
- μ΅μμ ν΄λμ€μμ
virtual
λ‘ μ μλ λ©μλλ₯Ό μμλ°λ ν΄λμ€μμsealed
νμ μμoverride
λ‘ μ μΈνκ²λλ©΄ κ·Έ ν΄λμ€λ₯Ό μμλ°λ ν΄λμ€μμλ ν΄λΉ λ©μλλ₯Ό μ¬μ μ ν μ μλ€.- νμ ν΄λμ€μμ μ€λ²λΌμ΄λ ν μ μλ€.
sealed
λ©μλλ κΌ overrideμμμλ§ μ¬μ©ν μ μλ€.sealed
λ₯Ό μ°λ©΄ μμμ μ€ μ μμΌλ―λ‘ νμ ν΄λμ€μμ μ¬μ μ λΆκ°λ₯
νλ³ν
as
,()
=> κΈ°λ³Έ μλ£νμΌλ‘ λ³ννμ κ²½μ°μλ ? +is
C# κΈ°λ³Έ μλ£ν λΆλ₯
κ΅¬λΆ | μ€λͺ |
---|---|
μ μ νν | byte, shortm int, long |
μ€μ νν | float, double |
λ¬Έμ νν | char |
μ°Έ, κ±°μ§ νν | boolean |
class Human
{
public void showInfo()
{
Console.WriteLine("Human");
}
}
class Programmer : Human
{
public void programming()
{
Console.WriteLine("κ°λ°μ");
}
}
class Program
{
static void Main(string[] args)
{
Human human = new Programmer();
// as μ°μ°μλ₯Ό μ΄μ©ν΄ κΈ°λ³Έ μλ£νμΌλ‘ νλ³ν
var aa = human as Programmer;
if (aa is null) return;
int? a = human as int;
var result = human is int; // false
// as μ°μ°μλ μ°Έμ‘° νμ λλ null νμ© νμκ³Ό ν¨κ» μ¬μ©ν΄μΌ ν©λλ€. 'int'μ(λ) nullμ νμ©νμ§ μλ κ° νμμ
λλ€.
string? str = human as string;
// μ°Έμ‘° λ³ν, boxing λ³ν, unboxing λ³ν, λν λ³ν λλ null νμ λ³νμ ν΅ν΄ 'AsisEx.Human' νμμ 'string'(μΌ)λ‘ λ³νν μ μμ΅λλ€.
// =>`as`μ°μ°μλ₯Ό μ¬μ©ν λ³νμ μμ, μ°Έμ‘° λ³ν λ° boxing λ³νμμ νμ©
int x = (int)human;
// => 'Human' νμμ 'int' νμμΌλ‘ λ³νν μ μμ΅λλ€.
// double y = (double)human; // => 'Human' νμμ 'double' νμμΌλ‘ λ³νν μ μμ΅λλ€.
// string str2 = (string)human; // => 'Human' νμμ 'string' νμμΌλ‘ λ³νν μ μμ΅λλ€.
Console.WriteLine(human is int);
Console.WriteLine(human is double);
Console.WriteLine(human is string);
}
}
Output
False
False
False
- μ°Έμ‘°νμ κΈ°λ³Έμλ£νμΌλ‘ λ³νμ΄ λΆκ°λ₯νλ€.(as λ° κ°μ νλ³ν λλ€ μλ¨(
()
μ¬μ©)) => C# μ체μμ λ§κ³ μμ is
λ₯Ό μ¬μ©νλκ²μ νλ³νμ΄ μλ bool νμ μ λ°ν κ°μ ν΅ν΄ 체ν¬λ₯Ό νλκ²μ΄κΈ°μ μ¬μ©κ°λ₯- stringμ κ²½μ° μ°Έμ‘°νμ΄μ§λ§ κΈ°λ³Έμλ£νμ²λΌ C#μμ λ§κ³ μλλ―ν¨.
μ΄κΈ°νλ κ°μ²΄μ μ΄κΈ°νλμ§ μμ κ°μ²΄λ₯Ό μμμ λ©μλμ λ§€κ°λ³μλ‘ λκ²Όμ λ λμνμΈ
class MyField
{
public int field1;
public int field2;
}
class AssignRefType
{
static void Main(string[] args)
{
MyField? mf1 = null;
MyField mf2 = new MyField() { field1 = 1, field2 = 2 };
Test(mf1);
Test(mf2);
//Console.WriteLine("mf1 => field1 : {0} field2 : {1} ", mf1.field1, mf1.field2); => error
//Console.WriteLine("mf2 => field1 : {0} field2 : {1} ", mf2.field1, mf2.field2);
TestRef(ref mf1);
TestRef(ref mf2);
Console.WriteLine("mf1 => field1 : {0} field2 : {1} ", mf1.field1, mf1.field2);
Console.WriteLine("mf2 => field1 : {0} field2 : {1} ", mf2.field1, mf2.field2);
}
private static void Test(MyField param)
{
if (param is null) param = new MyField();
param.field1 = 3;
param.field2 = 4;
}
private static void TestRef(ref MyField param)
{
if (param is null) param = new MyField();
param.field1 = 3;
param.field2 = 4;
}
}
Output
mf1 => field1 : 3 field2 : 4
mf2 => field1 : 3 field2 : 4
mf1
μ κ²½μ° stackλ©λͺ¨λ¦¬μμnull
μ κ°μ§κ³ heapμ λ©λͺ¨λ¦¬κ° ν λΉλμ΄μμ§ μλ€. C#μ κΈ°λ³Έμ μΌλ‘Call by Value
μ΄κΈ° λλ¬Έμnull
μ λ©μλλ‘ λκ²¨μ£Όκ² λκ³ μ§μμΈμ€ν΄μ€λ‘ νμ©λλparam
μ΄ λ΄λΆμμλ§ λ‘μ§μ μννκ³ νμ μΈμ€ν΄μ€κ° μμ λκΈ° λλ¬Έμ κ²°κ΅mf1
μ κ·Έλλ‘ nullμ κ°μ§κ² λλ€.
λ€νμ± νμΈ μμ μΆκ°
class A{
public virtual void Print1()
{
Console.WriteLine("A : Print1");
}
public void Print2()
{
Console.WriteLine("A : Print2");
}
}
class A1 : A
{
public override void Print1()
{
Console.WriteLine("A1 : Print1");
}
public void Print2()
{
Console.WriteLine("A1 : Print2");
}
}
class A2 : A
{
public override void Print1()
{
Console.WriteLine("A2 : Print1");
}
public void Print2()
{
Console.WriteLine("A2 : Print2");
}
}
class A3 : A
{
public override void Print1()
{
Console.WriteLine("A3 : Print1");
}
public void Print2()
{
Console.WriteLine("A3 : Print2");
}
}
class App
{
static void Main(string[] args)
{
A[] arr = new A[]
{
new A1(),
new A2(),
new A3()
};
foreach (var a in arr)
{
a.Print1();
a.Print2();
}
}
}
Output
A1 : Print1
A : Print2
A2 : Print1
A : Print2
A3 : Print1
A : Print2
- Aμ μλ£νμΌλ‘ A1, A2, A3 μΈμ€ν΄μ€λ₯Ό μμ±νμλ Aμμλ μμ ν΄λμ€μ λ©μλλ₯Ό μ°Έμ‘°ν μ μμΌλ, override ν€μλλ₯Ό μ΄μ©ν΄μ μ¬μ μ λ λ©μλμ κ²½μ° μμλ©μλμ κ²°κ³Όλ‘ μΆλ ₯μ΄ λλ€.
- μ¦ overrideμ μν΄ μ¬μ μλ λ©μλλ λΆλͺ¨ν΄λμ€νμ μΈμ€ν΄μ€λ₯Ό μμν΄λμ€νμ μΈμ€ν΄μ€λ‘ μ μΈνμλ μ¬μ μλ ννλ‘ μ¬μ©μ΄ κ°λ₯νλ€.
struct
μ ν¬κΈ°κ° μΌμ ν¬κΈ°λ₯Ό λμ΄κ°λ©΄ value type
μ΄ μλ ref type
=> Stack Overflow
λ©λͺ¨λ¦¬μ μ½μ ν΄λμ€
, μλλ ꡬ쑰체
- μΈμ€ν΄μ€νλ€λ©΄ κ°λΉμ§ 컬λ μ
(μλ)
- ꡬ쑰체λ μ€νμ λ°λ‘ ν λΉλκΈ° λλ¬Έμ κ°λ°μ§μ»¬λ μ μ΄ λ°μ νμ§ μμ
- ν΄λμ€λ μΈμ€ν΄μ€λ₯Ό μμ±ν λ λ§λ€ νμ λ©λͺ¨λ¦¬ ν λΉνκΈ° λλ¬Έμ κ°μ νκΈ°νκΈ° μν΄μλ κ°λ°μ§μ»¬λ μ μ΄ νμ.
- λ§μ μμ λ³μλ₯Ό κ°μ§κ³ μλ ꡬ쑰체λ NO(λ©λͺ¨λ¦¬)
- μ°Έμ‘°νμμΈ ν΄λμ€λ κ°λ€μ κ°λ¦¬ν€λ μ£Όμλ§ μ€νμ μ μ₯νμ§λ§ ꡬ쑰체λ κ°μ§κ³ μλ λ³μλ€μ κ°λ€μ λͺ¨λ μ€νμ μ μ₯νκΈ° λλ¬Έμ κ·Έ ν¬κΈ°λ§νΌ μ€νμ μμΉ μμ 컀μ§κ² λ¨. νμ§λ§ μ€νμ ν¬κΈ°κ° μ νμ μ΄κΈ° λλ¬Έμ λ무 λ§μ μμ κ°μ§κ² λλ©΄ μ€ν μ€λ²νλ‘μ°κ° λ°μν μ μλ μνμ΄ μλ€. => νλ©λͺ¨λ¦¬ μΉ¨λ²
- ꡬ쑰체λ μΈμ μ¬μ©ν κΉ?
- λ³μμ ν¬κΈ°κ° μκ±°λ, μλͺ μ΄ μ§§κ³ , μμ£Ό ν λΉλλ κ°μ²΄λ κ΅¬μ‘°μ²΄λ‘ λ§λ€μ΄ μ£Όλκ² μ’μ.