C#/C#200제
[C#] 6일차 - 37. 변수의 초기화와 default
반나무
2021. 1. 21. 14:33
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값을 출력합니다.
반응형