[C#] 6일차 - 37. 변수의 초기화와 defaultC#/C#200제2021. 1. 21. 14:33
Table of Contents
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A037_Default
{
class Program
{
enum E { Red, Green, Blue };
static void Main(string[] args)
{
int a = default;
string s = default;
Console.WriteLine("a = " + a); // 초기화된 int는 0
Console.WriteLine("s = " + s); // 초기화된 string은 null
Console.WriteLine("E = " + default(E)); // 0번째 enum을 출력
Console.WriteLine("E = " + (E)0);
MyList<int> iList = new MyList<int>();
Console.WriteLine("iList : " + iList.GetList());
MyList<string> sList = new MyList<string>();
Console.WriteLine("sList : " + sList.GetList());
}
}
public class MyList<T>
{
private class Node
{
public T data;
public Node next;
}
private Node head = default;
public void AddNode(T t)
{
Node newNode = new Node();
newNode.next = head;
newNode.data = t;
head = newNode;
}
public T GetList()
{
T temp = default(T);
Node current = head;
while ( current != null)
{
temp = current.data;
current = current.next;
}
return temp;
}
}
}
연결 리스트인 제네릭 클래스 MyList의 객체로 정수의 연결 리스트 iList를 생성합니다.
iList.GetLast() 메소드로 마지막 노드의 data값을 가져오는데 MyList가 비어있다면 data의 default값을 출력합니다.
반응형
'C# > C#200제' 카테고리의 다른 글
[C#] 6일차 - 39. object 타입과 boxing, unboxing (0) | 2021.01.21 |
---|---|
[C#] 6일차 - 38. Nullable형 (0) | 2021.01.21 |
[C#] 6일차 36. Null 조건 연산자(?) (0) | 2021.01.21 |
[C#] 6일차 - 35. 배열과 객체를 메소드 매개변수로 전달 (0) | 2021.01.21 |
[C#] 6일차 - 34. 값 형식과 참조 형식, ref 키워드 (0) | 2021.01.21 |
@반나무 :: 반나무_뿌리
3년차 WPF 개발자입니다.
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!