728x90
<์์ 1>
using System;
using System.Collections;
namespace CopyingArray
{
class MainApp
{
static void CopyArray<T>(T[] source, T[] target)
{
for (int i = 0; i < source.Length; i++)
target[i] = source[i];
}
static void Main(string[] args)
{
int[] source = {1,2,3,4,5};
int[] target = new int[source.Length];
CopyArray<int>(source, target);
foreach (var element in target) Console.WriteLine(element);
string[] source2 = { " one", " two", " three", " four", " five", " six", " seven", " ! You make me Eleven~" };
string[] target2 = new string[source2.Length];
CopyArray<string>(source2, target2);
foreach (var element in target2) Console.Write(element);
}
}
}
<์์ 1 ์ถ๋ ฅ๊ฒฐ๊ณผ>
1
2
3
4
5
one two three four five six seven ! You make me Eleven~
<์์ 2>
using System;
using System.Collections;
namespace Generic
{
class MyList<T>
{
private T[] array;
public MyList()
{
array = new T[3];
}
public T this[int index]
{
get
{
return array[index];
}
set
{
if(index >= array.Length)
{
Array.Resize<T>(ref array, index + 1);
Console.WriteLine($"Array Resize : {array.Length}");
}
array[index] = value;
}
}
public int Length
{
get { return array.Length; }
}
}
class MainApp
{
static void Main(string[] args)
{
MyList<string> strList = new MyList<string>();
strList[0] = "One";
strList[1] = " Two";
strList[2] = " Three";
strList[3] = " Four";
strList[4] = " Five";
strList[5] = " Six";
strList[6] = " Seven";
strList[7] = " ! You Make Me Eleven.~~~";
for (int i = 0; i < strList.Length; i++)
Console.Write(strList[i]);
MyList<Object> objectList = new MyList<Object>();
objectList[0] = 1;
objectList[1] = 2;
objectList[2] = 3;
objectList[3] = 4;
objectList[4] = 5;
objectList[5] = 6;
objectList[6] = 7;
objectList[7] = " ! You Make Me Eleven.~~~";
for(int i=0; i< objectList.Length; i++)
{
Console.Write("\t");
Console.Write(objectList[i]);
}
MyList<int> intList = new MyList<int>();
intList[0] = 1;
intList[1] = 2;
intList[2] = 3;
intList[3] = 4;
intList[4] = 5;
intList[5] = 6;
intList[6] = 7;
intList[7] = 11;
for (int i = 0; i < intList.Length; i++)
{
Console.Write("\t");
Console.Write(intList[i]);
}
}
}
}
<์์ 2 ์ถ๋ ฅ๊ฒฐ๊ณผ>
Array Resize : 4
Array Resize : 5
Array Resize : 6
Array Resize : 7
Array Resize : 8
One Two Three Four Five Six Seven ! You Make Me Eleven.~~~Array Resize : 4
Array Resize : 5
Array Resize : 6
Array Resize : 7
Array Resize : 8
1 2 3 4 5 6 7 ! You Make Me Eleven.~~~Array Resize : 4
Array Resize : 5
Array Resize : 6
Array Resize : 7
Array Resize : 8
1 2 3 4 5 6 7 11
<์์ 3> -> ์ด ์์ ๊ฐ ์ดํดํ๊ธฐ ์กฐ๊ธ ๊น๋ค๋ก์ ์
using System;
using System.Collections;
namespace ConstraintssOnTypeParameters
{
class StructArray<T> where T : struct
{
public T[] Array { get; set; }
public StructArray(int size)
{
Array = new T[size];
}
}
class Refarray<T> where T : class
{
public T[] Array { get; set; }
public Refarray(int size)
{
Array = new T[size];
}
}
class Base { }
class Derived : Base { }
class BaseArray<U> where U : Base
{
public U[] Array { get; set; }
public BaseArray(int size)
{
Array = new U[size];
}
public void CopyArray<T>(T[] Source)where T:U
{
Source.CopyTo(Array, 0);
}
}
class MainApp
{
public static T CreateInstance<T>() where T:new()
{
return new T();
}
static void Main(string[] args)
{
StructArray<int> a = new StructArray<int>(3);
a.Array[0] = 0;
a.Array[1] = 1;
a.Array[2] = 2;
Refarray<StructArray<double>> b = new Refarray<StructArray<double>>(3);
b.Array[0] = new StructArray<double>(5);
b.Array[1] = new StructArray<double>(10);
b.Array[2] = new StructArray<double>(1005);
BaseArray<Base> c = new BaseArray<Base>(3);
c.Array[0] = new Base();
c.Array[1] = new Derived();
c.Array[2] = CreateInstance<Base>();
BaseArray<Derived> d = new BaseArray<Derived>(3);
d.Array[0] = new Derived(); //Base ํ์์ ์ฌ๊ธฐ์ ํ ๋นํ ์ ์๋ค.
d.Array[1] = new Derived();
d.Array[2] = CreateInstance<Derived>();
BaseArray<Derived> e = new BaseArray<Derived>(3);
e.CopyArray<Derived>(d.Array);
}
}
}
<์์ 3 ์ถ๋ ฅ๊ฒฐ๊ณผ>
์ฝ์ ์ฐ์ ๊ฒ์ด ์์
728x90
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
59. foreach๋ฅผ ์ฌ์ฉํ ์ ์๋ ์ผ๋ฐํ ํด๋์ค (0) | 2022.02.28 |
---|---|
58. ์ผ๋ฐํ ์ปฌ๋ ์ (0) | 2022.02.28 |
56. ์ผ๋ฐํํ ๋, ํ์ ๋งค๊ฐ๋ณ์ ์ ์ฝ์ํค๊ธฐ (0) | 2022.02.28 |
55. foreach๊ฐ ๊ฐ๋ฅํ ๊ฐ์ฒด ๋ง๋ค๊ธฐ (0) | 2022.02.27 |
54. ์ธ๋ฑ์ (0) | 2022.02.25 |