using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cs24_generic
{
#region <일반화 클래스>
class MyArray<T> where T : class
{
T[] array;
}
#endregion
internal class Program
{
#region <일반화 메소드>
static void CopyArray<T>(T[] source, T[]target)
{
for (var i = 0; i < source.Length; i++)
{
target[i] = source[i];
}
}
#endregion
static void Main(string[] args)
{
#region < 일반화시키기 >
int[] source = { 2, 4, 6, 8, 10 };
int[] target = new int[source.Length];
CopyArray(source, target);
foreach(var item in target)
{
Console.WriteLine(item);
}
Console.WriteLine();
long[] source2 = { 2100000, 2300000, 3300000, 5600000, 7800000 };
long[] target2 = new long[source2.Length];
CopyArray(source2, target2);
foreach (var item in target2)
{
Console.WriteLine(item);
}
Console.WriteLine();
float[] source3 = { 3.14f, 3.15f, 3.16f };
float[] target3 = new float[source3.Length];
CopyArray(source3, target3);
foreach (var item in target3)
{
Console.WriteLine(item);
}
Console.WriteLine();
#endregion
List<int> list = new List<int>();
for(var i = 10; i>0; i--)
{
list.Add(i);
}
foreach(var item in list)
{
Console.WriteLine(item);
}
Console.WriteLine();
list.RemoveAt(3);
foreach(var item in list)
{
Console.WriteLine(item);
}
Console.WriteLine();
list.Sort();
foreach (var item in list)
{
Console.WriteLine(item);
}
Console.WriteLine();
List<MyArray<string>> myStringArray = new List<MyArray<string>>();
Stack<float> stFloats = new Stack<float>();
stFloats.Push(3.14f);
stFloats.Push(7.26f);
stFloats.Push(1.01f);
while (stFloats.Count > 0)
{
Console.WriteLine(stFloats.Pop());
}
Console.WriteLine();
Queue<string> qStrings = new Queue<string>();
qStrings.Enqueue("Hello");
qStrings.Enqueue("World");
qStrings.Enqueue("My");
qStrings.Enqueue("C#");
while(qStrings.Count > 0)
{
Console.WriteLine(qStrings.Dequeue());
}
Console.WriteLine() ;
Dictionary<string, int> dicNumbers = new Dictionary<string, int>();
dicNumbers["하나"] = 1;
dicNumbers["둘"] = 2;
dicNumbers["셋"] = 3;
dicNumbers["넷"] = 4;
Console.WriteLine(dicNumbers["셋"]);
}
}
}
예외처리
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cs25_exception
{
internal class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3 };
try
{
for (var i = 0; i < 5; i++)
{
Console.WriteLine(array[i]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("계속 진행");
}
try
{
DivideTest(array[2], 0);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("프로그램 종료");
}
private static void DivideTest(int v1, int v2)
{
try
{
Console.WriteLine(v1 / v2);
}
catch (Exception)
{
throw new Exception("DivideTest 메서드에서 예외가 발생");
}
Console.WriteLine(v1 / v2);
}
}
}