C# ๋ณตํฉ ๋ฐ์ดํฐ ํ์์๋ ํด๋์ค ๋ง๊ณ ๋ ๊ตฌ์กฐ์ฒด๋ผ๋ ๊ฒ์ด ์๋ค.
๊ตฌ์กฐ์ฒด๋ ํด๋์ค์ ์ฌ์ด์ง๊ฐ์ฏค ๋๊ธฐ ๋๋ฌธ์ ํ๋์ ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ์๋ ๋ฑ ์๋น ๋ถ๋ถ ๋น์ทํ๋ค.
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ
{
//ํ๋
//๊ตฌ์กฐ์ฒด
}
๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ธํ๋ ์
struct MyStruct
{
public int MyField1
public int MtField2
public void MyMethod()
{
//...
}
}
<๊ตฌ์กฐ์ฒด์ public ํ๋>
๊ตฌ์กฐ์ฒด์ ํ๋๋ฅผ public์ผ๋ก ์ ์ธํ๋ค.
ํด๋์ค์์๋ ์๋์ฑ๋๋ฌธ์ ๋ชจ๋ ํ๋๋ฅผ ๋ฐ๋์ private๋ก ์์ฑํ์๋ค.
๋ฌธ๋ฒ์ ์ผ๋ก๋ ๊ตฌ์กฐ์ฒด๊ฐ ํด๋์ค์ ์ ์ฌํ๊ธฐ๋ ํด๋, ์กด์ฌ ์ด์ ๊ฐ ๋ค๋ฅด๋ค.
ํด๋์ค๋ ์ค์ธ๊ณ์ ๊ฐ์ฒด๋ฅผ ์ถ์ํํ๋ ค๋๋ฐ ๊ทธ ์กด์ฌ์ ์ด์ ๊ฐ ์์ง๋ง
๊ตฌ์กฐ์ฒด๋ ๋ฐ์ดํฐ๋ฅผ ๋ด๊ธฐ ์ํ ์๋ฃ๊ตฌ์กฐ๋ก ์ฌ์ฉ๋๋ค.
(๋ฐ๋ผ์ ๊ตณ์ด ์๋์ฑ์ ๋น๋กฏํ ๊ฐ์ฒด์งํฅ์ ์์น์ ๊ตฌ์กฐ์ฒด์ ๊ฐํ๊ฒ ์ ์ฉํ์ง๋ ์๋ ํธ์ด๋ฉฐ,
ํธ์๋ฅผ ์ํด ํ๋๋ฅผ public์ผ๋ก ์ ์ธํด์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.)
ํน์ง | ํด๋์ค | ๊ตฌ์กฐ์ฒด |
ํค์๋ | class | struct |
ํ์ | ์ฐธ์กฐ ํ์(ํ์ ํ ๋น) | ๊ฐ ํ์(์คํ์ ํ ๋น) |
๋ณต์ฌ | ์์ ๋ณต์ฌ(Shallow Copy) | ๊น์ ๋ณต์ฌ(Deep Cpoy) |
์ธ์คํด์ค ์์ฑ | new ์ฐ์ฐ์์ ์์ฑ์ ํ์ | ์ ์ธ๋ง์ผ๋ก๋ ์์ฑ |
์์ฑ์ | ๋งค๊ฐ๋ณ์ ์๋ ์์ฑ์ ์ ์ธ ๊ฐ๋ฅ | ๋งค๊ฐ๋ณ์ ์๋ ์์ฑ์ ์ ์ธ ๋ถ๊ฐ๋ฅ |
์์ | ๊ฐ๋ฅ | ๊ฐ ํ์์ด๋ฏ๋ก ์์ ๋ถ๊ฐ๋ฅ |
ํด๋์ค๋ ์ฐธ์กฐ ํ์์ด๊ณ ๊ตฌ์กฐ์ฒด๋ ๊ฐ ํ์์ด๋ค.(์ ์ผ ์ค์ํ ์ฐจ์ด)
-> ๋ฐ๋ผ์ ๊ตฌ์กฐ์ฒด์ ์ธ์คํด์ค๋ ์คํ์ ํ ๋น๋๊ณ ์ธ์คํด์ค๊ฐ ์ ์ธ๋ ๋ธ๋ก์ด ๋๋๋ ์ง์ ์ ๋ฉ๋ชจ๋ฆฌ์์ ์ฌ๋ผ์ง๋ค.
-> ํด๋์ค์ ๋นํด์ ์ฑ๋ฅ์ ์ด์ ์ด ์๋ค : ์ธ์คํด์ค์ ์ฌ์ฉ์ด ๋๋๋ฉด ์ฆ์ ๋ฉ๋ชจ๋ฆฌ์์ ์ ๊ฑฐ๋๋ค๋ ์ ,
๊ฐ๋น์ง ์ฝ๋ ํฐ๋ฅผ ๋ ๊ท์ฐฎ๊ฒ ํ๋ค๋ ์
(์๋ฅผ ๋ค์ด 3์ฐจ์ ๊ฒ์์ ๋ง๋๋๋ฐ ํ๋ฉด์ ๋ํ๋ด์ผ ํ ์๋ง์ ์ขํ๋ค์ ์ฌ์ฉ์ด ๋๋๋ฉด ์ฆ์ ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ๋๋ ๊ฒ๊ณผ ๋ฉ๋ชจ๋ฆฌ์ ๊ทธ๋๋ก ๋จ์ ๊ฐ๋น์ง ์ฝ๋ ํฐ๋ฅผ ๊ดด๋กญํ๋ ๊ฒ๋ง ๋น๊ตํด๋ด๋ ๊ตฌ์กฐ์ฒด๋ ์ด์ ์ด ์๋ค)
๊ตฌ์กฐ์ฒด๋ ๊ฐ ํ์์ด๊ธฐ ๋๋ฌธ์ ํ ๋น ์ฐ์ฐ์ =๋ฅผ ํตํด ๋ชจ๋ ํ๋๊ฐ ๊ทธ๋๋ก ๋ณต์ฌ๋๋ค.
๋ค์์ ๊ตฌ์กฐ์ฒด ๋ณต์ฌ์ ์์ด๋ค.
MyStruct s;
s.MyField1 =1;
s.MtFiedl2 =2;
MtStruct t;
t = s;
s.MyField1 =3;
<์์ ์ฝ๋>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using MyExtension;
namespace MyExtension
{
public static class IntegerExtension
{
public static string Append(this string myString, string appendStr)
{
string thisStr = myString;
return thisStr += appendStr;
}
public static int Square(this int myInt)
{
return myInt * myInt;
}
public static int Power(this int myInt, int exponent)
{
int result = myInt;
for (int i = 1; i < exponent; i++)
result = result * myInt;
return result;
}
}
}
namespace ExtensionMethod
{
struct MyStruct
{
public int MyField1;
public int MyField2;
}
class MainApp
{
static void Main(string[] args)
{
MyStruct s;
s.MyField1 = 1;
s.MyField2 = 2;
MyStruct t;
t = s;
s.MyField1 = 3;
WriteLine($"s์ MyField1 : {s.MyField1} MyField2๋ : {s.MyField2}"); //s์ MyField1 : 3 MyField2๋ : 2
WriteLine($"t์ MyField1 : {t.MyField1} MyField2๋ : {t.MyField2}"); //t์ MyField1 : 1 MyField2๋ : 2
}
}
}
<์์ ์ฝ๋2>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Structure
{
struct Point3D
{
public int X;
public int Y;
public int Z;
public Point3D(int X, int Y, int Z) //struct๋ด ์ ์ธ๋ ๋ณ์๋ค์ ๋ค ํ ๋น์ ํด์ฃผ์ด์ผ ์ปดํ์ผ ์๋ฌ๊ฐ ๋จ์ง ์์์ ํ์ธํ์์
{
this.X = X;
this.Y = Y;
this.Z = Z;
}
public override string ToString()//๊ธฐ์กด System.Object ํ์์ ToString() ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ
{
return string.Format($"{X}, {Y}, {Z}");
}
}
class MainApp
{
static void Main(string[] args)
{
Point3D p3d1; //์ ์ธ๋ง์ผ๋ก๋ ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ค.
p3d1.X = 10;
p3d1.Y = 20;
p3d1.Z = 40;
WriteLine(p3d1.ToString()); //10, 20, 40
Point3D p3d2 = new Point3D(100,200,300); //์์ฑ์๋ฅผ ์ด์ฉํ ์ธ์คํด์ค ์์ฑ๋ ๊ฐ๋ฅํ๋ค.
Point3D p3d3 = p3d2;//๊ตฌ์กฐ์ฒด์ ์ธ์คํด์ค๋ฅผ ๋ค๋ฅธ ์ธ์คํด์ค์ ํ ๋นํ๋ฉด ๊น์ ๋ณต์ฌ๊ฐ ์ด๋ฃจ์ด์ง๋ค.
p3d3.Z = 500;
WriteLine(p3d2.ToString()); //100, 200, 300
WriteLine(p3d3.ToString()); //100, 200, 500
}
}
}
๊ฐ์ฒด๋ ์์ฑ๊ณผ ๊ธฐ๋ฅ์ผ๋ก ์ด๋ฃจ์ด์ง๋ค.
์์ฑ์ ์ํ, ๊ธฐ๋ฅ์ ํ์๋ผ๊ณ ๋ ํ๋ค.
๊ฐ์ฒด์ ์์ฑ์ ํ๋์ ํ๋กํผํฐ๋ฅผ ํตํด ํํ๋๋ฏ๋ก,
๊ฐ์ฒด์ ์ํ ๋ํ ํ๋์ ํ๋กํผํฐ๋ฅผ ํตํด ํํ๋๋ค๊ณ ํ ์ ์๋ค.
์ํ์ ๋ณํ๋ฅผ ํ์ฉํ๋ ๊ฐ์ฒด๋ฅผ ๋ณ๊ฒฝ๊ฐ๋ฅ ๊ฐ์ฒด๋ผ๊ณ ํ๋ฉฐ,
์ํ์ ๋ณํ๋ฅผ ํ์ฉํ์ง ์๋ ๊ฐ์ฒด๋ฅผ ๋ณ๊ฒฝ๋ถ๊ฐ๋ฅ ๊ฐ์ฒด๋ผ๊ณ ํ๋ค.
๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ ๊ฐ์ฒด์ ํจ์ฉ:
(1) ๋ฉํฐ ์ฐ๋ ๋ ๊ฐ์ ๋๊ธฐํ๋ฅผ ํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ํ๋ก๊ทธ๋จ ์ฑ๋ฅ ํฅ์์ด ๊ฐ๋ฅ
(2) ๋ฒ๊ทธ๋ก ์ธํ ์ํ(๋ฐ์ดํฐ)์ ์ค์ผ์ ๋ง์ ์ ์์
(3) ํ๋ก๊ทธ๋จ์ ๋ฒ๊ทธ๋ฅผ ์ ๋ฐ์ํค๋ ์ํ ์ค์ผ์ ์์ธ์ ์ฐพ๊ธฐ ์ํด ์๋ง์ ์ค๋ ๋๋ฅผ ๋๋ฒ๊น ํด์ผ ํ๋ ์๊ณ ์ ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํด ์ฝ๋๋ฅผ ์ฅ์ด์ง์ผ ํ๋ ๊ดด๋ก์์ ๊ฒช์ง ์์๋ ๋จ
* ๊ตฌ์กฐ์ฒด๋ ๋ชจ๋ ํ๋์ ํ๋กํผํฐ์ ๊ฐ์ ์์ ํ ์ ์๋, ์ฆ ๋ณ๊ฒฝ๋ถ๊ฐ๋ฅ ๊ตฌ์กฐ์ฒด๋ก ์ ์ธํ ์ ์๋ค.
(์ด์ ๋ฐํด ๊ตฌ์กฐ์ฒด๋ ๋ณ๊ฒฝ๋ถ๊ฐ๋ฅ์ผ๋ก ์ ์ธํ ์ ์๋ค)
๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ธํ ๋ readonly ํค์๋๋ง ๊ธฐ์ ํ๋ฉด ๋๋ค.
readonly struct ๊ตฌ์กฐ์ฒด์ด๋ฆ
{
}
readonly๋ฅผ ์ด์ฉํด์ ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ธํ๋ฉด, ์ปดํ์ผ๋ฌ๋ ํด๋น ๊ตฌ์กฐ์ฒด์ ๋ชจ๋ ํ๋๊ฐ readonly๋ก ์ ์ธ๋๋๋ก ๊ฐ์ ํ๋ค.
readonly๋ก ์ ์ธ๋ ๊ตฌ์กฐ์ฒด ์์์
readonly๋ก ์ ์ธ๋์ง ์์ ํ๋์ ํ๋กํผํฐ๋ ์ปดํ์ผ ์๋ฌ๋ฅผ ์ผ์ผํจ๋ค.
readonly struct InnutableStruct
{
public readonly int ImmmutableField; //OK
public int MutableField; //์ปดํ์ผ ์๋ฌ
}
์๋์ ๊ฐ์ด ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์ ์ธ๋ ํ๋๋ฅผ ์์ ํ๋ ค๋ ์๋์ ๋ํด์๋ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Structure
{
readonly struct ImmmutableStruct
{
public readonly int ImmmutableField; //OK
public ImmmutableStruct(int initValue)
{
ImmmutableField = initValue; //์์ฑ์์์๋ง ์ด๊ธฐํ ๊ฐ๋ฅ
}
public void TestMethod(int testValue)
{
ImmmutableField = testValue; //์ปดํ์ผ ์๋ฌ. ์์ฑ์์์๋ง ์ด๊ธฐํ ๊ฐ๋ฅ
}
}
class SomeClasss
{
public void SomeMethod()
{
ImmmutableStruct imStruct = new ImmmutableStruct(123);
imStruct.ImmmutableField = 456;//์ปดํ์ผ ์๋ฌ!
}
}
class MainApp
{
static void Main(string[] args)
{
}
}
}
<์์ 3>
์๋ ์ฝ๋๋ ๋ถ๋ณ ๊ฐ์ฒด์ ์ํ๋ฅผ ๊ฑด๋๋ฆฌ๋ ค๊ณ ํ๋ฏ๋ก ์ปดํ์ผ์ ์คํจํ ์๋ฐ์ ์๋ค.
Red ๊ฐ์ฒด์ ๋๋จธ์ง ๊ฐ์ ์ ์งํ๋ฉด์ G์ ๊ฐ๋ง 100์ธ ๊ฐ์ฒด๋ฅผ ์ป์ผ๋ ค๋ฉด?
๊ฐ๋จํ๋ค. '์๋ก์ด' ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋ฉด ๋๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ReadOnlyStructure
{
readonly struct RGBColor
{
public readonly byte R; //OK
public readonly byte G; //OK
public readonly byte B; //OK
public RGBColor(byte r, byte g, byte b)
{
R = r;
G = g;
B = b;
}
}
class MainApp
{
static void Main(string[] args)
{
RGBColor Red = new RGBColor(255,0,0);
Red.G = 100;//์ปดํ์ผ ์๋ฌ
}
}
}
์๋ก์ด ๊ฐ์ฒด
RGBColor MyColor = new RGBColor(Red.R, Red.G, Red.B);
<์ปดํ์ผ ์๋ฌ ์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ReadOnlyMethod
{
struct ACsetting
{
public double curInCelsius; //ํ์ฌ ์จ๋(c)
public double target; //ํฌ๋ง ์จ๋
public readonly double GetFahrenheit() //readonly๋ก ํ์ ํ ๋ฉ์๋์์ ๊ฐ์ฒด์ ํ๋๋ฅผ ๋ฐ๊พธ๋ ค๊ณ ํ๋ฉด ์ปดํ์ผ ์๋ฌ ๋ฐ์
{
//ํ์จ(F) ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ target์ ์ ์ฅ
target = curInCelsius * 1.8 + 32;
return target;
}
}
class MainApp
{
static void Main(string[] args)
{
ACsetting acs;
acs.curInCelsius = 25;
acs.target = 25;
WriteLine($"{acs.curInCelsius}");
WriteLine($"{acs.target}");
}
}
}
public double target; //ํฌ๋ง ์จ๋
public readonly double GetFahrenheit() //readonly๋ก ํ์ ํ ๋ฉ์๋์์ ๊ฐ์ฒด์ ํ๋๋ฅผ ๋ฐ๊พธ๋ ค๊ณ ํ๋ฉด ์ปดํ์ผ ์๋ฌ ๋ฐ์
{
//ํ์จ(F) ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ target์ ์ ์ฅ
target = curInCelsius * 1.8 + 32;
return target;
}
GetFahrenheit() ํจ์ ์์์
target = curInCelsius * 1.8 + 32; ๋ถ๋ถ์ด ์๋ฌ๊ฐ ๋๋ ์ด์ ๋
์ฝ๊ธฐ ์ ์ฉ์ธ readonly ํจ์์ธ๋ฐ target ๊ฐ์ ๋ณ๊ฒฝํ๋ ค๊ณ ํด์์ด๋ค.
๊ทธ๋์ ๊ตฌํ์
return curInCelsius * 1.8 + 32;๋ก ๋ณ๊ฒฝํ๋ฉด ์ ์ ์๋๋๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ReadOnlyMethod
{
struct ACsetting
{
public double curInCelsius; //ํ์ฌ ์จ๋(c)
public double target; //ํฌ๋ง ์จ๋
public readonly double GetFahrenheit() //readonly๋ก ํ์ ํ ๋ฉ์๋์์ ๊ฐ์ฒด์ ํ๋๋ฅผ ๋ฐ๊พธ๋ ค๊ณ ํ๋ฉด ์ปดํ์ผ ์๋ฌ ๋ฐ์
{
//ํ์จ(F) ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ target์ ์ ์ฅ
return (curInCelsius * 1.8) + 32;
}
}
class MainApp
{
static void Main(string[] args)
{
ACsetting acs;
acs.curInCelsius = 25;
acs.target = 25;
WriteLine($"{acs.GetFahrenheit()}");
WriteLine($"{acs.target}");
}
}
}
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ด๊ฒ์ด C#์ด๋ค 7์ฅ ์ฐ์ต๋ฌธ์ ์ฐธ๊ณ (0) | 2022.02.14 |
---|---|
32. ํํ (0) | 2022.02.14 |
30. ํ์ฅ ๋ฉ์๋ (0) | 2022.02.14 |
29. ๋ถํ ํด๋์ค (0) | 2022.02.11 |
28. ์ค์ฒฉ ํด๋์ค (0) | 2022.02.11 |