728x90
반응형
[예제1]
using System.Collections.Generic;
using static System.Console;
namespace ConsoleApp1
{
class Tiger
{
private int a, b;
public int A
{
get
{
return a;
}
set
{
a = value;
}
}
public int B
{
get
{
return b;
}
set
{
b = value;
}
}
}
class Program
{
static void Main(string[] args)
{
// public 문법으로 사용할 수 있는 것이다.
// private로 바꾸면서 Property를 이용한다.
Tiger t = new Tiger()
{
A = 10, B = 20
};
WriteLine(t.A + " " + t.B);
}
}
}
[예제2]
using System.Collections.Generic;
using static System.Console;
namespace ConsoleApp1
{
class Tiger
{
private int a, b;
public int A
{
get
{
return a;
}
set
{
a = value;
}
}
public int B
{
get
{
return b;
}
set
{
b = value;
}
}
}
class Program
{
static void Main(string[] args)
{
// var obj = new Tiger();
// 익명클래스 또는 무명클래스
var obj = new { Name = "홍길동", Age = 30 };
WriteLine(obj.Name);
WriteLine(obj.Age);
// 변수가 상수화 된다.
//obj.Age = 100;
var b = new { Subject = "수학", Scores = new int[] { 1, 2, 3, 4 } };
WriteLine(b.Subject);
foreach(var item in b.Scores)
{
Write(item + " ");
}WriteLine();
}
}
}
728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
[C# 언어 3강] 데이터형 (5/5) 값 형식과 참조 형식, 정리 (0) | 2021.02.23 |
---|---|
# 27) [C#] 문법9 (array) (0) | 2021.02.22 |
# 26.2) [C#] 문법7 (interface) (0) | 2021.02.19 |
# 26.1) [C#] 문법6 (0) | 2021.02.19 |
# 25.2) [C#] 문법5 (Class) (0) | 2021.02.18 |