반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발꿈나무

8/11(수) 3일차 C#.Net 교육 본문

.Net 교육

8/11(수) 3일차 C#.Net 교육

HYOKYE0NG 2021. 8. 11. 18:22
반응형

반복문


//String group object
//string strS = "Hello, World!";

//String - interpolation
string strS = $"Hello, World!   {1}   {2}";

WriteLine(strS);

// loof statement_CPU Service
for(int i = 0; i < strS.Length; i++)
{
    Write(strS[i]);
}
WriteLine();

int idx = 0;
while (idx < strS.Length)
{
    Write(strS[idx++]);
}
WriteLine();


// iterator statement_Collection(group object) Service
// foreach: C# keyword(not service) -> CLR이 Service로 Converter
foreach (var kkk in strS) //var: local variable(type 지정 필요없이 CLR이 알아서 TYPE 지정)
{
    Write(kkk);
}
WriteLine();


// Dynamic coding
// Interface operation_IoC(Inversion Of Control)
IEnumerator pointer = strS.GetEnumerator();
while (pointer.MoveNext()==true)
{
    Write(pointer.Current);
}
WriteLine();


// interactive operation(user가 interaction): tightly-coupled = data + UI(foregroud-thread = CPU Code)
//fluent API(LINQ service): full stack service, HTML5
strS.ToList().ForEach(kkk => Write(kkk));
WriteLine();

//reactive operation(data가 interaction): loosely-coupled(divide and concure) = data / UI, GPU solution = parallel operation + concurrency
//fluent API(LINQ service): full stack service, HTML5
strS.ToObservable().Subscribe(kkk => Write(kkk));
WriteLine();

<result>

Hello, World!   1   2
Hello, World!   1   2
Hello, World!   1   2
Hello, World!   1   2

Hello, World!   1   2

Hello, World!   1   2

Hello, World!   1   2

 

var: 개발자가 type 지정할 필요 없이 CLR이 run-time에 알아서 type 지정해 줌

   (ex) var num = 10; --> int num = 10;

 

 

 

-----

C# 2.0: foreach is dead = collection service deading

C# 3.0 later: LINQ query service - prediction, confiting

Aggregate: foreach가 사라지면서 LINQ의 새로운 기능??

 

3E: expactation, experience, evaluation

    - odd condition operation - expactation(infomation), experience(data)

    - REPL service(dynamic operation): evaluation

 

 

-----

Array

// Single Variable/constant = data ==> new Activator utility service 호출

int suja = 10; //primitive dataType: 할당(data allocation)할 때
int suja2 = new int(); //new: C# keyword -> operation으로 변환 필요 / new를 만나면 utility 필요
int suja3 = (int)System.Activator.CreateInstance(typeof(int));

suja2 = suja;
suja3 = suja;

WriteLine($"suja: {suja}, suja2: {suja2}, suja3: {suja3}");


// Group Variable/constant => Array utility service 호출
// group array style object
//1. declare
int[] aaas; // jagged array
int[] aaas2; // rectangle array style = matrix

//WriteLine($"{aaas}, {aaas2}"); 선언만 하고 정의 내리지 않았기 때문에 에러남

//2. define
aaas = new int[100];
aaas2 = (int[])System.Array.CreateInstance(typeof(int), 100);
WriteLine($"{aaas}, {aaas2}");


//3. reference

//set, group
Enumerable.Range(1, 101).ToList().ForEach(i => aaas[i] = i + 1);
Enumerable.Range(1, 101).ToList().ForEach(i => aaas2[i] = i + 1);

aaas.ToList().ForEach(i => Write(i));
WriteLine();

aaas2.ToObservable().Subscribe(i => Write(i));
WriteLine();

 

Array Initilization

//class -> property / group -> indexer service
using System;

int[] sus = new int[10];
int[] sus1 = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //initialization -> construct service의 목적(초기화)
int[] sus2 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //동적 operation: 배열의 크기 정해놓지 않음
int[] sus3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };


//3. reference - left reference
for (int i=0; i<10; i++)
{
    sus[i] = i + 1;
}

//3. reference - right reference
for (int i = 0; i < 10; i++)
{
    Write($"   {sus[i]}");
}
WriteLine();

void OneDimensionDisplay(Span<int> aaa) // Span: pointer(likely $) -> 대용량 데이터에서도 사용 가능
//void OneDimensionDisplay(int[] aaa)
{
    foreach (var kkk in aaa)
    {
        Write($"   {kkk}");
    }
    WriteLine();
}

OneDimensionDisplay(sus1);
OneDimensionDisplay(sus2);
OneDimensionDisplay(sus3);

 

//class -> property / group -> indexer service

// 1차원 배열
int[] sus1 = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //initialization -> construct service의 목적(초기화)
int[] sus2 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //동적 operation: 배열의 크기 정해놓지 않음
int[] sus3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// 2차원 배열
int[][] suss = new int[][] { sus, sus1, sus2, sus3 };
int[][] suss1 = new int[][] { sus, sus1, sus2, sus3 };
int[][] suss2 = new int[][] { sus, sus1, sus2, sus3 };
int[][] suss3 = new int[][] { sus, sus1, sus2, sus3 };
int[][] suss4 = new int[][] { sus, sus1, sus2, sus3 };

//3차원 배열
int[][][] susss = new int[][][] { suss, suss1, suss2, suss3, suss4 };
int[][][] susss1 = new int[][][] { suss, suss1, suss2, suss3, suss4 };
int[][][] susss2 = new int[][][] { suss, suss1, suss2, suss3, suss4 };
int[][][] susss3 = new int[][][] { suss, suss1, suss2, suss3, suss4 };
int[][][] susss4 = new int[][][] { suss, suss1, suss2, suss3, suss4 };


//3. reference - left reference
for (int i=0; i<10; i++)
{
    sus[i] = i + 1;
}

//3. reference - right reference
for (int i = 0; i < 10; i++)
{
    Write($"   {sus[i]}");
}
WriteLine();

// 1차원 배열 출력 함수
void OneDimensionDisplay(Span<int> aaa) // Span: pointer(likely $) -> 대용량 데이터에서도 사용 가능
//void OneDimensionDisplay(int[] aaa)
{
    foreach (var kkk in aaa)
    {
        Write($"   {kkk}");
    }
    WriteLine();
}

OneDimensionDisplay(sus1);
OneDimensionDisplay(sus2);
OneDimensionDisplay(sus3);


//2차원 배열 출력
foreach(var kkk in suss)
{
    foreach(var kk in kkk)
    {
        Write($"   {kk}");
    }
    WriteLine();
}
WriteLine("--------------------------------");

foreach (var kkk in suss1)
{
    OneDimensionDisplay(kkk);
}

foreach (var kkk in suss2)
{
    OneDimensionDisplay(kkk);
}

foreach (var kkk in suss3)
{
    OneDimensionDisplay(kkk);
}
WriteLine("----------------------");


// 3차원 배열 출력
foreach (var kkk in susss)
{
    foreach (var kk in kkk)
    {
        OneDimensionDisplay(kk);
        /*foreach (var k in kk)
        {
            Write($"   {k}");
        }
        WriteLine();*/
    }
}
WriteLine("--------------");


// 2차원 배열인데 왜 이렇게 한 건지는 모르겠음
int[] sus = new int[10];
int[,] ssus = new int[10, 10];

for (int i = 0; i< 10; i++)
{
    for(int j = 0; j<10; j++)
    {
        ssus[i, j] = j + 1;
    }
}

for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        Write($"   {ssus[i, j]}");
    }
    WriteLine();
}

WriteLine("-------------------");

 

 

-----

Array Utility

  - using service 사용 -> library

  - using service 사용X -> utility

int[] ss = (int[])System.Array.CreateInstance(typeof(int), 10);
int[] sus1 = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// Array Utility
Array.Copy(sus1, ss, 8);
foreach (var kkk in ss)
{
    Write($"   {kkk}");
}
WriteLine();

Array.Reverse(ss);
foreach (var kkk in ss)
{
    Write($"   {kkk}");
}
WriteLine();

Array.Sort(ss);
foreach (var kkk in ss)
{
    Write($"   {kkk}");
}
WriteLine();

Array.Clear(ss, 3, 3);
foreach (var kkk in ss)
{
    Write($"   {kkk}");
}
WriteLine();

<result>

   1   2   3   4   5   6   7   8   0   0
   0   0   8   7   6   5   4   3   2   1
   0   0   1   2   3   4   5   6   7   8
   0   0   1   0   0   0   5   6   7   8

 

 

-----

DataSet, DataTable, DataColumn, DataRow

// Table service([][]): code-first -> graphQL
global using System.Data;
global using System.Data.Common;

DataSet ds = new(); // store servcie = repository(in-memoryDB)

DataTable t1 = new(); // detaching service(store 입장)
DataTable t2 = new();
DataTable t3 = new();

ds.Tables.Add(t1); // attaching servcie(store 입장)
ds.Tables.Add(t2);
ds.Tables.Add(t3);
WriteLine($"{ds.Tables[0]}   {ds.Tables[1]}   {ds.Tables[2]}");

ds.Tables.Remove(t1); // detach시킴
ds.Tables.Remove(t3);
WriteLine($"{t1}   {t2}   {t3}");


// code-first before
DataColumn id = new DataColumn("id", typeof(int));
DataColumn age = new DataColumn("age", typeof(int));
DataColumn name = new DataColumn("name", typeof(string));
DataColumn gender = new DataColumn("gender", typeof(bool));
DataColumn address = new DataColumn("address", typeof(ADDR));

// column attaching
t2.Columns.Add(id);
t2.Columns.Add(age);
t2.Columns.Add(name);
t2.Columns.Add(gender);
t2.Columns.Add(address);


bool toggle = false; //gender
DataRow row = t2.NewRow();

// row attaching
for(int i = 0; i < 10; i++)
{
    row[0] = i + 1;
    row[1] = (i + 1) * 10;
    row[2] = "helele" + (i + 1);
    row[3] = toggle;
    row[4] = ((i + 1) % 5) switch
    {
        0 => ADDR.DAEGU,
        1 => ADDR.JEJU,
        2 => ADDR.KWANGJU,
        3 => ADDR.PUSAN,
        4 => ADDR.SEOUL,
        _ => ADDR.SEOUL
    };

    t2.Rows.Add(row);
    row = t2.NewRow();
    toggle = !toggle;
}

for (int i=0; i<10; i++)
{
    WriteLine($"ID: {t2.Rows[i][0]}, Age: {t2.Rows[i][1]}, Name: {t2.Rows[i][2]}, Gender: {t2.Rows[i][3]}, Address: {t2.Rows[i][4]}");
}


enum ADDR
{
    SEOUL,
    PUSAN,
    KWANGJU,
    DAEGU,
    JEJU
}

<result>

Table1   Table2   Table3
Table1   Table2   Table3
ID: 1, Age: 10, Name: helele1, Gender: False, Address: 4
ID: 2, Age: 20, Name: helele2, Gender: True, Address: 2
ID: 3, Age: 30, Name: helele3, Gender: False, Address: 1
ID: 4, Age: 40, Name: helele4, Gender: True, Address: 0
ID: 5, Age: 50, Name: helele5, Gender: False, Address: 3
ID: 6, Age: 60, Name: helele6, Gender: True, Address: 4
ID: 7, Age: 70, Name: helele7, Gender: False, Address: 2
ID: 8, Age: 80, Name: helele8, Gender: True, Address: 1
ID: 9, Age: 90, Name: helele9, Gender: False, Address: 0
ID: 10, Age: 100, Name: helele10, Gender: True, Address: 3

 

 

-----

Tuning

  - group을 javascript처럼 작업하는 방법: 1.Generics, 2. IoC, 3.Delegate

      - Generics(C# 2.0 later): 1. generic function, 2. generic class, 3. generic collection

            - generic function: data-type와 business-code를 loosely-coupled(Interface: class와 class를 loosely-couped)

 

 

 

 

- function은 code로 이루어져 있는데 code의 default는 thread(primary)

 

 

 

-----

Generic Function: data-type와 business-code를 loosely-coupled(Interface: class와 class를 loosely-couped)

WriteLine(Add(10, 20));
WriteLine(Add(100, 200));
WriteLine(Add(1000, 2000));

WriteLine(Add("10", "20"));
WriteLine(Add("100", "200"));
WriteLine(Add("1000", "2000"));

WriteLine(Add(10.0f, 20.0f));
WriteLine(Add(100.0f, 200.0f));
WriteLine(Add(1000.0f, 2000.0f));

WriteLine(Add(10.0m, 20.0m));
WriteLine(Add(100.0m, 200.0m));
WriteLine(Add(1000.0m, 2000.0m));


//generic function
dynamic Add<T>(T s1, T s2) // T: Type의 약자, <>: bracket(여기서는 함수 level)
{
    dynamic d1 = s1;
    dynamic d2 = s2;
    
    return d1 + d2;
}

<result>

30
300
3000
1020
100200
10002000
30
300
3000
30.0
300.0
3000.0

 

 

 

-----

Generic Class

HeleleClass<int> obj = new HeleleClass<int>();
WriteLine(obj.Add(10, 20));
WriteLine(obj.Add(100, 200));
WriteLine(obj.Add(1000, 2000));

HeleleClass<string> obj2 = new HeleleClass<string>(); //un-boxing, loosely-coupled(쓰고자 하는 순간에 type 지정해서 사용 가능)
WriteLine(obj2.Add("10", "20"));
WriteLine(obj2.Add("100", "200"));
WriteLine(obj2.Add("1000", "2000"));

HeleleClass<float> obj3 = new HeleleClass<float>();
WriteLine(obj3.Add(10.0f, 20.0f));
WriteLine(obj3.Add(100.0f, 200.0f));
WriteLine(obj3.Add(1000.0f, 2000.0f));

HeleleClass<decimal> obj4 = new HeleleClass<decimal>();
WriteLine(obj4.Add(10.0m, 20.0m));
WriteLine(obj4.Add(100.0m, 200.0m));
WriteLine(obj4.Add(1000.0m, 2000.0m));


partial class HeleleClass<T> // generic class, <>: bracket(여기서는 클래스 level)
{
    //generic function
    public T Add(T s1, T s2) // T: Type의 약자
    {
        dynamic d1 = s1;
        dynamic d2 = s2;

        return d1 + d2;
    }

    public T Sub(T s1, T s2) // T: Type의 약자
    {
        dynamic d1 = s1;
        dynamic d2 = s2;

        return d1 - d2;
    }

    public T Mul(T s1, T s2) // T: Type의 약자
    {
        dynamic d1 = s1;
        dynamic d2 = s2;

        return d1 * d2;
    }

    public T Div(T s1, T s2) // T: Type의 약자
    {
        dynamic d1 = s1;
        dynamic d2 = s2;

        return d1 / d2;
    }
}

 

 

 

-----

Generic Collection

// C# 1.0 lIST
ArrayList colle = new();
colle.Add(1);
colle.Add(2);

for (int i = 3; i <= 10; i++) colle.Add(i);

Write($"   {colle[0]}");
Write($"   {colle[1]}");

foreach (var kkk in colle) Write($"   {kkk}");
WriteLine();

// C# 2.0 Generic Collection - list
List<int> colle2 = new();
colle2.Add(1);
colle2.Add(2);

for (int i = 3; i <= 10; i++) colle2.Add(i);

Write($"   {colle2[0]}");
Write($"   {colle2[1]}");

foreach (var kkk in colle2) Write($"   {kkk}");
WriteLine();



// C# 1.0 stack
Stack colle3 = new();
colle3.Push(1);
colle3.Push(2);

for (int i = 3; i <= 10; i++) colle3.Push(i);

Write($"   {colle3.Pop()}");
Write($"   {colle3.Pop()}");

foreach (var kkk in colle3) Write($"   {kkk}");
WriteLine();

// C# 2.0 Generic Collection - Stack
Stack<int> colle4 = new();
colle4.Push(1);
colle4.Push(2);

for (int i = 3; i <= 10; i++) colle4.Push(i);

Write($"   {colle4.Pop()}");
Write($"   {colle4.Pop()}");

foreach (var kkk in colle4) Write($"   {kkk}");
WriteLine();


// C# 1.0 Queue
Queue colle5 = new();
colle5.Enqueue(1);
colle5.Enqueue(2);

for (int i = 3; i <= 10; i++) colle5.Enqueue(i);

Write($"   {colle5.Dequeue()}");
Write($"   {colle5.Dequeue()}");

foreach (var kkk in colle5) Write($"   {kkk}");
WriteLine();

// C# 2.0 Generic Collection - Queue
Queue<int> colle6 = new();
colle6.Enqueue(1);
colle6.Enqueue(2);

for (int i = 3; i <= 10; i++) colle6.Enqueue(i);

Write($"   {colle6.Dequeue()}");
Write($"   {colle6.Dequeue()}");

foreach (var kkk in colle6) Write($"   {kkk}");
WriteLine();

<result>

   1   2   1   2   3   4   5   6   7   8   9   10
   1   2   1   2   3   4   5   6   7   8   9   10
   10   9   8   7   6   5   4   3   2   1
   10   9   8   7   6   5   4   3   2   1
   1   2   3   4   5   6   7   8   9   10
   1   2   3   4   5   6   7   8   9   10

 

 

Dictionary

// C# 1.0 Dictionary[key, value] - search service
Hashtable colle55 = new();
colle55["aaa"] = 1111;
colle55["bbb"] = 2222;
colle55["ccc"] = 3333;
colle55["ddd"] = 4444;
colle55["eee"] = 5555;
colle55["fff"] = 6666;

WriteLine($"   {colle55["ccc"]}");
WriteLine($"   {colle55["ddd"]}");

foreach (DictionaryEntry kkk in colle55) WriteLine($"   KEY: {kkk.Key}, VALUE: {kkk.Value}");
WriteLine();


// C# 2.0 Dictionary[key, value] - search service
Dictionary<String, int> colle66 = new();
colle66["aaa"] = 1111;
colle66["bbb"] = 2222;
colle66["ccc"] = 3333;
colle66["ddd"] = 4444;
colle66["eee"] = 5555;
colle66["fff"] = 6666;

WriteLine($"   {colle66["ccc"]}");
WriteLine($"   {colle66["ddd"]}");

foreach (var kkk in colle66) WriteLine($"   KEY: {kkk.Key}, VALUE: {kkk.Value}");
WriteLine();


// C# 6.0 Dictionary[key, value] - search service
Dictionary<String, int> colle666 = new Dictionary<string, int> //constructor call(초기화 service 위해)
{
    ["aaa"] = 1111, 
    ["bbb"] = 2222,
    ["ccc"] = 3333,
    ["ddd"] = 4444,
    ["eee"] = 5555,
    ["fff"] = 6666 // batching operation
}; // 초기화 하면서 thread 줄일 수 있음


WriteLine($"   {colle666["ccc"]}");
WriteLine($"   {colle666["ddd"]}");

foreach (var kkk in colle666) WriteLine($"   KEY: {kkk.Key}, VALUE: {kkk.Value}");
WriteLine();


// C# 8.0 later Dictionary[key, value] - search service
// 사용하는 순간(real-time call)에 만들어서 사용(미리 만들어 놓은 것: static)
// global using System.Text.Json; text값을 영구 저장장치로 가지고 있겠다(global.cs file에 저장)

var colle77 = JsonSerializer.Serialize(colle666); //6.0 group을 문자열로 저장

WriteLine(colle77);
WriteLine();

var colle88 = JsonSerializer.Deserialize<Dictionary<String, int>>(colle77); // 저장된 객체를 사용될 시점에 올려놓음
foreach (var kkk in colle88) WriteLine($"   KEY: {kkk.Key}, VALUE: {kkk.Value}");
WriteLine();

<result>

   3333
   4444
   KEY: aaa, VALUE: 1111
   KEY: ccc, VALUE: 3333
   KEY: fff, VALUE: 6666
   KEY: ddd, VALUE: 4444
   KEY: bbb, VALUE: 2222
   KEY: eee, VALUE: 5555

   3333
   4444
   KEY: aaa, VALUE: 1111
   KEY: bbb, VALUE: 2222
   KEY: ccc, VALUE: 3333
   KEY: ddd, VALUE: 4444
   KEY: eee, VALUE: 5555
   KEY: fff, VALUE: 6666

   3333
   4444
   KEY: aaa, VALUE: 1111
   KEY: bbb, VALUE: 2222
   KEY: ccc, VALUE: 3333
   KEY: ddd, VALUE: 4444
   KEY: eee, VALUE: 5555
   KEY: fff, VALUE: 6666

{"aaa":1111,"bbb":2222,"ccc":3333,"ddd":4444,"eee":5555,"fff":6666}

   KEY: aaa, VALUE: 1111
   KEY: bbb, VALUE: 2222
   KEY: ccc, VALUE: 3333
   KEY: ddd, VALUE: 4444
   KEY: eee, VALUE: 5555
   KEY: fff, VALUE: 6666

 

 

Immutable Collection

// mutable operation
//List<int> helele = new() { 1, 2, 3, 4, 5 };
var helele = (new List<int> { 1, 2, 3, 4, 5 }).ToImmutableList<int>(); // mutable -> immutable, global using System.Collections.Immutable;
IReadOnlyList<int> helele2 = new List<int> { 1, 2, 3, 4, 5 } // mutable -> immutable, global using System.Collections.Immutable;

//Dictionary<int, string> hevele = new()
//{
//    { 1, "a" },
//    { 2, "b" },
//    { 3, "c" },
//    { 4, "d" },
//    { 5, "e" },
//};
var hevele = (new Dictionary<int, string> // mutable -> immutable, global using System.Collections.Immutable;
{
    { 1, "a" },
    { 2, "b" },
    { 3, "c" },
    { 4, "d" },
    { 5, "e" },
}).ToImmutableDictionary<int, string>();
IReadOnlyDictionary<int, string> hevele2 = new Dictionary<int, string> // mutable -> immutable, global using System.Collections.Immutable;
{
    { 1, "a" },
    { 2, "b" },
    { 3, "c" },
    { 4, "d" },
    { 5, "e" },
};

foreach (var kkk in helele)
{
    Write($"   {kkk}");
}
WriteLine();

foreach (var kkk in hevele)
{
    WriteLine($"   Key: {kkk.Key}, Value: {kkk.Value}");
}
WriteLine();


// List 값 변경(ToImmutableList일 때) - error는 안 나지만 add는 안됨
// List 값 변경(IReadOnlyList일 때) - ERROR
//helele2.Add(6);
//helele2.Add(7);
//helele2.Add(8);

//// Dictionary 값 변경(ToImmutableDictionary일 때, IReadOnlyDictionary일 떄) - error
//hevele2[6] = "f";
//hevele2[7] = "g";


// mutable -> 값 변경 가능, immutable -> 값 변경 불가능
//helele = new() { 10, 20, 30, 40, 50 };
//hevele = new() { { 10, "a" }, { 20, "b" }, { 30, "c" }, { 40, "d" }, { 50, "e" } };

foreach (var kkk in helele)
{
    Write($"   {kkk}");
}
WriteLine();

foreach (var kkk in hevele)
{
    WriteLine($"   Key: {kkk.Key}, Value: {kkk.Value}");
}
WriteLine();

 

 

 

 

-----

Bad Coding
   - static operation --> dynamic operation
   - heavy-weight operation(spagetti code) --> light-weight operation(framework)
   - tightly-coupled --> loosely-coupled

 

반응형

'.Net 교육' 카테고리의 다른 글

8/17(월) 6일차 C#.Net 교육  (0) 2021.08.17
8/13(금) 5일차 C#.Net 교육  (0) 2021.08.13
8/12(목) 4일차 C#.Net 교육  (0) 2021.08.12
MVC application  (0) 2021.08.10
8/10(화) 2일차 C#.Net 교육  (0) 2021.08.10
Comments