์๋ ํ๋กํผํฐ
๊ธฐ๋ฅ์C# 3.0
์์ ๋์ ๋ ๊ธฐ๋ฅ
๊ธฐ์กด ํ๋กํผํฐ ์ฌ์ฉ ์ฝ๋
public string Name
{
get { return name; }
set { name = value; }
}
public DateTime EntryDate
{
get { return entryDate; }
set { entryDate = value; }
}
C# 3.0 ์ดํ ์๋ ํ๋กํผํฐ
์ฌ์ฉ ์ฝ๋
public string Name
{
get; set;
}
public DateTime EntryDate
{
get; set;
}
- C# 7.0 ๋ถํฐ๋ ์๋ ํ๋กํผํฐ์ ์ด๊ธฐ๊ฐ์ด ํ์ํ ๋ ์์ฑ์์ ์ด๊ธฐํ ์ฝ๋๋ฅผ ์์ฑํด์ผํ๋ ๋ถํธํจ์ ํด์ํ ์ ์๋๋ก ์ด๊ธฐ๊ฐ์ ๋ฐ๋ก ์ค์ ํ ์ ์๋ค.
- ์๋ ์ฝ๋๋ฅผ ํตํด ํ์ธํด๋ณด์
๊ธฐ์กด C# 3.0์์์ ์๋ ํ๋กํผํฐ ํด๋์ค
class EmployeeInfo
{
public string Name
{
get;
set;
}
public DateTime EntryDate
{
get;
set;
}
// ๊ทผ์ ์ฐ์๋ฅผ ๊ตฌํจ.
public int ServiceLength
{
// Tick(ํฑ) => ์ฒ๋ง๋ถ์ 1์ด = 100 nano second
// 1 mili second = 10,000 ํฑ
get { return new DateTime(DateTime.Now.Subtract(EntryDate).Ticks).Year; }
}
}
class Program {
static void Main(string[] args)
{
EmployeeInfo employeeInfo1 = new EmployeeInfo()
{
Name = "ํ๊ธธ๋",
EntryDate = new DateTime(2011, 10, 11)
};
Console.WriteLine($"์ด๋ฆ : {employeeInfo1.Name}");
Console.WriteLine($"์
์ฌ์ผ : {employeeInfo1.EntryDate.ToShortTimeString()}");
Console.WriteLine($"๊ทผ์์ฐ์ : {employeeInfo1.ServiceLength}");
}
}
Output
์ด๋ฆ : ํ๊ธธ๋
์
์ฌ์ผ : ์ค์ 12:00
๊ทผ์์ฐ์ : 11
C# 7.0์์์ ์๋ํ๋กํผํฐ ํด๋์ค
class EmployeeInfo
{
public string Name { get; set; } = "์๋ฌด๋"; //์ด๊ธฐ๊ฐ ์ค์
public DateTime EntryDate { get; set; } = new DateTime(2000,1,1);
// ๊ทผ์ ์ฐ์๋ฅผ ๊ตฌํจ.
public int ServiceLength { get { return new DateTime(DateTime.Now.Subtract(EntryDate).Ticks).Year; } }
}
- C# 7.0 ๋ถํฐ๋ ํ๋กํผํฐ๋ฅผ ์ ์ธ๊ณผ ๋์์ ์์ฑํ๋ฉฐ ์ด๊ธฐ๊ฐ๋ ์ค์ ํ ์ ์๋๋ก ๋ณํํ์๋ค.
๊ฐ์ฒด ์์ฑ์ ๋ ๋ค๋ฅธ ํ๋ ์ด๊ธฐํ ๋ฐฉ๋ฒ
static void Main(string[] args)
{
EmployeeInfo employeeInfo1 = new EmployeeInfo()
{
Name = "ํ๊ธธ๋",
EntryDate = new DateTime(2011, 10, 11)
};
Console.WriteLine($"์ด๋ฆ : {employeeInfo1.Name}");
Console.WriteLine($"์
์ฌ์ผ : {employeeInfo1.EntryDate.ToShortTimeString()}");
Console.WriteLine($"๊ทผ์์ฐ์ : {employeeInfo1.ServiceLength}");
//๊ธฐ์กด์ ์๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ
EmployeeInfo employeeInfo2 = new EmployeeInfo();
Console.WriteLine($"์ด๋ฆ : {employeeInfo2.Name}");
Console.WriteLine($"์
์ฌ์ผ : {employeeInfo2.EntryDate.ToShortTimeString()}");
Console.WriteLine($"๊ทผ์์ฐ์ : {employeeInfo2.ServiceLength}");
employeeInfo2.Name = "๊ณ ๊ธธ๋";
employeeInfo2.EntryDate = new DateTime(2005, 5, 25);
Console.WriteLine($"์ด๋ฆ : {employeeInfo2.Name}");
Console.WriteLine($"์
์ฌ์ผ : {employeeInfo2.EntryDate.ToShortTimeString()}");
Console.WriteLine($"๊ทผ์์ฐ์ : {employeeInfo2.ServiceLength}");
}
- ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ๊ฐ์ฒด์ ํ๋๋ฅผ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ => ์ ์ธ๋ฐฉ๋ฒ
ํด๋์ค๋ช ์ธ์คํด์ค๋ช = new ํด๋์ค๋ช () { ํ๋กํผํฐ1์ด๋ฆ = ๊ฐ, ํ๋กํผํฐ2์ด๋ฆ = ๊ฐ, ... }
```
'๐ป Programming Language > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
20. ์ถ์ํด๋์ค์์ ์๋ ํ๋กํผํฐ ์ฌ์ฉ๋ฒ (0) | 2022.04.07 |
---|---|
19. ์ธํฐํ์ด์ค์์ ์๋ ํ๋กํผํฐ ์ฌ์ฉ๋ฒ (0) | 2022.04.07 |
17. ํ๋กํผํฐ(Property)์ ์ดํด (0) | 2022.04.07 |
16. ์ถ์ํด๋์ค(Abstract Class) ์ดํด (0) | 2022.04.06 |
15. ์ธํฐํ์ด์ค(Interface) ์ดํด (0) | 2022.04.06 |