struct property

์ด๋ฒˆ์—” ๊ฐ„ํ˜น ๋†“์น ๋•Œ๊ฐ€ ์žˆ๋Š” ๊ธฐ์ดˆ์ ์ธ ๋‚ด์šฉ์— ๋Œ€ํ•ด ์ ์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

c#์—์„œ struct๋ฅผ property๋กœ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ ํ•ด์•ผํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

internal class Program
{
	static void Main(string[] args)
	{
		var test = new TestClass();
		test.Foo();
	}
}

public class TestClass
{
	public TestStruct Val1 { get; set; }
	public TestStruct Val2;
	private TestStruct _val;
	public TestStruct Val3
	{
		get { return _val; }
		set { _val = value; }
	}

	public void Foo()
	{
		Val1.SetValue();
		Console.WriteLine(Val1.Value);

		Val2.SetValue();
		Console.WriteLine(Val2.Value);

		Val3.SetValue();
		Console.WriteLine(Val3.Value);

		_val.SetValue();
		Console.WriteLine(Val3.Value);
	}
}

public struct TestStruct
{
	public int Value;
    public void SetValue()
	{
		Value = 10;
	}
}

์œ„ ์†Œ์Šค๋ฅผ ์‹คํ–‰ ํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์ƒ ๋˜์‹œ๋‚˜์š”?

โ€‹

10

10

10

10

์ด ๋‚˜์˜ฌ๊ฑฐ ๊ฐ™์ง€๋งŒ

โ€‹

์‹ค์ œ ๊ฒฐ๊ณผ๋Š”

0

10

0

10

์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.

โ€‹

์›์ธ์€ c# ์—์„œ property์˜ get์€

์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฒฝ์šฐ ํ•ด๋‹น ์ฐธ์กฐ๋ฅผ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ ํ•˜์ง€๋งŒ

๊ฐ’ ํƒ€์ž…์˜ ๊ฒฝ์šฐ ๋ณต์‚ฌ๋ณธ์„ ์ „๋‹ฌ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

โ€‹

Val1.SetValue(); ์˜ ๊ฒฝ์šฐ

๋ณต์‚ฌ ๋œ ์ž„์‹œ TestStruct ์˜ Value ๋ฅผ 10์œผ๋กœ ์„ค์ • ํ•œ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—

์‹ค์ œ Val1์˜ Value๋Š” ๋ณ€ํ™”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

โ€‹

ํ•˜์ง€๋งŒ set์€ ์‹ค์ œ property์— ์ €์žฅ ๋˜๋ฏ€๋กœ

ํ—ค๊น”๋ฆฌ์ง€ ์•Š๋„๋ก ์ฃผ์˜ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

11 Likes

์กฐ๊ธˆ ๋‹ค๋ฅธ ์ฃผ์ œ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’ ํƒ€์ž…์€ ๊ฐ’์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋„๋ก ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์ฒ˜๋Ÿผ ๋™์ž‘ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋‹ค๋ฅด๊ฒŒ ๋งํ•˜๋ฉด ๋ถˆ๋ณ€ ๊ฐ์ฒด(immutable object)์—ฌ์•ผ ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด System.Int32, System.Char๊ณผ ๊ฐ™์€ ํƒ€์ž…๋“ค๋„ ๋ชจ๋‘ ๊ฐ’ ํƒ€์ž…์ธ๋ฐ ์œ„์™€ ๊ฐ™์€ ํ˜ผ๋ž€์„ ๊ฒช์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์™œ ๊ทธ๋Ÿด๊นŒ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ํ•ด๋‹น ํƒ€์ž…๋“ค์ด ๋ชจ๋‘ ๋ถˆ๋ณ€์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ฐ์ฒด๊ฐ€ ๋ถˆ๋ณ€์ด๋ผ๋Š” ๊ฒƒ์€ ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜์—ˆ๋“  ์‚ฌ๋ณธ์„ ํ†ตํ•ด ์ „๋‹ฌ๋˜์—ˆ๋“  ์–ธ์ œ๋‚˜ ์ƒํƒœ์˜ ๋ณ€ํ™”๊ฐ€ ์ „ํŒŒ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹น์—ฐํ•ฉ๋‹ˆ๋‹ค. ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๊ฒŒ ๋ง‰์•„๋’€๊ธฐ ๋•Œ๋ฌธ์— ์ƒํƒœ์˜ ๋ณ€ํ™”๊ฐ€ ์ „ํŒŒ๋ ์ง€๋ฅผ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ถˆ๋ณ€์„ฑ(immutability)์„ ํ”ํžˆ ๋ฌด์ƒํƒœ์„ฑ(statelessness)์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋ถˆ๋ณ€ ๊ฐ์ฒด๋Š” ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋“  ์‚ฌ๋ณธ์„ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋“  ์˜๋ฏธ๋ก ์˜ ๋ณ€ํ™”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ—ท๊ฐˆ๋ฆด ์ผ์ด ์—†๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

C#์—์„œ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ ์•„์˜ˆ ์ด ํƒ€์ž…์ด ๋ถˆ๋ณ€์„ฑ์„ ๊นจ๋œจ๋ฆฌ์ง€ ์•Š๋„๋ก ๊ฐ•์ œํ•  ์ˆ˜๋‹จ๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ readonly struct ํ‚ค์›Œ๋“œ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋“  ํ•„๋“œ๋ฅผ readonly๋กœ ์„ ์–ธํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ์„ฑ๋Šฅ์„ ์œ„ํ•ด ๋ณด์ด์ง€ ์•Š๋Š” ์บ์‹œ๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ณด์กดํ•ด์•ผ ํ•œ๋‹ค๊ฑฐ๋‚˜ ํ•˜๋Š” ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ฉด ๊ตฌ์กฐ์ฒด๋Š” ํ•ญ์ƒ readonly๋กœ๋งŒ ์„ ์–ธํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค.

7 Likes

vo(value object)์— ์ ํ•ฉํ•˜์ฃ