C# 기초 – csharpstudy 유튜브 강의 정리
https://www.youtube.com/watch?v=NZ0csWCxD3g&list=PLiNvMfa_Y5hfgpgd3hgXdHACCZuIEozjL
- C# 데이타 타입
- C# yield 문
- C# 구조체 (struct)
- C# 클래스 (class) 개념과 사용법
- C# 이벤트(event) 활용 사례
C# yield 문
yield return은 데이터를 하나씩 리턴한다.
yield break는 메소드를 끝낸다. (break 는 가장 가까운 반복문을 끝낸다.)
private void main()
{
foreach (var score in GetScores())
{
Console.WriteLine(score);
}
}
private IEnumerable<int> GetScores() // foreach 를 하나씩 돈다.
{
int[] scores = new int[] { 10, 20, 30, 40, 50 };
for (int i = 0; i < scores.Length; i++)
{
if (scores[i] == 30)
{
yield break; // 메소드를 끝냄.
// break // 가장 가까운 반복문을 끝냄.
}
yield return scores[i];
Console.WriteLine(i + " : Done");
}
}
C# 구조체 (struct)
https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/builtin-types/struct
public struct Coords
{
public Coords(double x, double y)
{
X = x;
Y = y;
}
public double X { get; }
public double Y { get; }
public override string ToString() => $"({X}, {Y})";
}
C# 클래스 (class) 개념과 사용법
public class Brick
{
// 필드 (데이타)
private int width;
private int height;
private int depth;
private Color color;
// 생성자 (Constructor)
public Brick()
{
width = 10;
height = 10;
depth = 5;
color = Color.Gray;
}
// Constructor that takes four arguments:
public Brick(int width, int height, int depth, Color color)
{
this.width = width;
this.height = height;
this.depth = depth;
this.color = color;
}
// 속성 (Property)
public int Width
{
get { return this.width; }
set
{
if (value > 0)
{
this.width = value;
}
}
}
public Color Color
{
get { return this.color; }
}
public int Volume
{
get { return width * height * depth; }
}
// 메서드 (Method)
public void MakeBrick()
{
// Event Fire
if (ProcessStarted != null)
{
ProcessStarted(this, EventArgs.Empty);
}
Step1();
Step2();
Step3();
if (ProcessCompleted != null)
{
ProcessCompleted(this, EventArgs.Empty);
}
}
private void Step1()
{
Console.WriteLine("Step #1");
}
private void Step2()
{
Console.WriteLine("Step #2");
}
private void Step3()
{
Console.WriteLine("Step #3");
}
// 이벤트
// 클래스 내부에서 일이 일어났을 때 외부에 알리는 역할
public event EventHandler ProcessStarted;
public event EventHandler ProcessCompleted;
}
public MainWindow()
{
Brick br = new Brick();
Brick br2 = new Brick(50, 100, 20, Color.Gray);
int w = br2.Width;
br2.Width = -1;
Color c = br2.Color;
int v = br2.Volume;
br2.ProcessStarted += Br2_ProcessStarted;
br2.ProcessCompleted += Br2_ProcessCompleted;
br2.MakeBrick();
}
private void Br2_ProcessStarted(object sender, EventArgs e)
{
Console.WriteLine("Process Started");
}
private void Br2_ProcessCompleted(object sender, EventArgs e)
{
Console.WriteLine("Process Completed");
}
C# 이벤트(event) 활용 사례
// 방법: 스레드로부터 안전한 호출을 Windows Forms 컨트롤 만들기
https://docs.microsoft.com/ko-kr/dotnet/desktop/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls?view=netframeworkdesktop-4.8
namespace FormsEventExercise
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCopy_Click(object sender, EventArgs e)
{
Thread t = new Thread(CopyFile);
t.Start();
}
private void CopyFile()
{
FileManager fm = new FileManager();
fm.InProgress += Fm_InProgress;
fm.InProgress += Fm_InProgress2;
fm.Copy("src.mp4", "destfile.mp4");
}
private void Fm_InProgress2(object sender, double e)
{
Debug.WriteLine("Progress {0}", e);
}
private void Fm_InProgress(object sender, double e)
{
// UI 쓰레드인지 확인
if (InvokeRequired)
{
Invoke(new EventHandler<double>(Fm_InProgress), sender, e);
}
else
{
this.progressBar1.Value = (int)e;
this.lblPct.Text = string.Format("{0} %", (int)e);
}
}
}
}
namespace FormsEventExercise
{
public class FileManager
{
public event EventHandler<double> InProgress;
public void Copy(string srcfile, string destfile)
{
byte[] buffer = new byte[1024];
int pos = 0;
// 파일 사이즈
var fi = new FileInfo(srcfile);
var fileSize = fi.Length;
// 파일 복사
using (BinaryReader rd = new BinaryReader(File.Open(srcfile, FileMode.Open)))
using (BinaryWriter wr = new BinaryWriter(File.Open(destfile, FileMode.Create)))
{
while (pos < fileSize)
{
int count = rd.Read(buffer, 0, buffer.Length);
wr.Write(buffer, 0, count);
pos += count;
double pct = (pos / (double)fileSize) * 100;
if (InProgress != null)
{
InProgress(this, pct);
}
}
}
}
}
}
C# 데이타 타입
bool b = true;
// signed integer
short sh = 100;
int i = 100;
long l = 10000L;
// unsigned integer
ushort us = 111;
uint ui = 110U;
ulong ul = 1000UL;
float f = 3.14f; // 32bit
double d = 3.141592d; // 64bit
decimal dd = 3.14M; // 128bit
char ch = '한'; // 16bit Unicode
byte by = 0x46; // 8bit
// nullable type
int? ix = null;
if (ix == null)
{
Console.WriteLine("ix is null");
}
else
{
Console.WriteLine(ix.Value);
}
string s = "Hello";
object o = null;