cpu안에 있는 메모리 - 레지스터(메모리 중에 가장 빠름)
*배열 선언 후 for문과 foreach문을 이용하여 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_001
{
class Program
{
static void Main(string[] args)
{
int[] Number1 = {1,3,5,7,11 };
for (int i = 0; i < Number1.Length; ++i)
{
Console.WriteLine(Number1[i]);
}
Console.WriteLine();
foreach (var Num in Number1)
{
Console.WriteLine(Num);
}
}
}
}
*메서드 호출
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_001
{
class Program
{
// int 배열을 불러오는 메서드
static void ArrayIntPrint(int[] Num)
{
for (int i = 0; i < Num.Length; ++i)
{
Console.WriteLine(Num[i]);
}
}
// double 배열을 불러오는 메서드
static void ArrayDoublePrint(Double[] Num)
{
for (int i = 0; i < Num.Length; ++i)
{
Console.WriteLine(Num[i]);
}
}
// String 배열을 불러오는 메서드
static void ArrayStringPrint(String[] Num)
{
for (int i = 0; i < Num.Length; ++i)
{
Console.WriteLine(Num[i]);
}
}
static void Main(string[] args)
{
// 메서드 호출하기
int[] Number1 = {1,3,5,7,11 };
ArrayIntPrint(Number1);
Console.WriteLine();
double[] Number2 = { 1.1, 3.1, 5.1, 7.1, 11.1 };
ArrayDoublePrint(Number2);
Console.WriteLine();
string[] Number3 = { "일","이","삼","사"};
ArrayStringPrint(Number3);
}
}
}
> 각 int, double, string 메서드에서 코드의 중복이 발생했다.
(각 메서드는 type은 다르지만 기능은 같다! > generic 사용가능!)
*코드의 중복제거(제너릭 - 일반화)
1. 메서드를 제너릭! (메서드 안 배열의 type만 다르고 기능은 똑같을 때)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_001
{
class Program
{
/* Generic(제너릭) - 일반화 --> 타입만 다르고 기능은 동일할때 사용!
type을 일반화 시켰다. */
static void ArrayPrint <T> (T[] Num) // <변수이름> : 제너릭부분을 메서드에 적용, <>의 위치가 중요하다!
{ // T = type의 약자, 대부분 이렇게 사용
for (int i = 0; i < Num.Length; ++i)
{
Console.Write("{0} ", Num[i]);
}
Console.WriteLine();
}
static void Main(string[] args)
{
int[] Number1 = { 1, 3, 5, 7, 11 };
double[] Number2 = { 1.1, 3.1, 5.1, 7.1, 11.1 };
string[] Number3 = { "일", "이", "삼", "사" };
// 제너릭 메서드 호출하는 방법 <> 사이에 내가 사용하고싶은 type을 적는다.
ArrayPrint<int>(Number1);
ArrayPrint<double>(Number2);
ArrayPrint<string>(Number3);
}
}
}
*배열을 복사하는 메서드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_003
{
class Program
{
static void ArrayIntCopy(int[] Dst, int[] Src)
{
for (int i = 0; i < Src.Length; ++i)
{
Dst[i] = Src[i];
Console.Write("{0} ", Dst[i]);
}
Console.WriteLine();
}
//static void ArrayPrint<T>(T[] Num)
//{
// for (int i = 0; i < Num.Length; ++i)
// {
// Console.Write("{0} ", Num[i]);
// }
// Console.WriteLine();
//}
static void Main(string[] args)
{
int[] ArrayInt1 = { 2, 4, 6, 8 };
int[] ArrayInt2 = new int[4];
ArrayIntCopy(ArrayInt2, ArrayInt1);
//ArrayPrint<int>(ArrayInt2);
}
}
}
- double형 배열
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_003
{
class Program
{
static void ArrayIntCopy(int[] Dst, int[] Src)
{
for (int i = 0; i < Src.Length; ++i)
{
Dst[i] = Src[i];
Console.Write("{0} ", Dst[i]);
}
Console.WriteLine();
}
static void ArrayDoubleCopy(double[] Dst, double[] Src)
{
for (int i = 0; i < Src.Length; ++i)
{
Dst[i] = Src[i];
Console.Write("{0} ", Dst[i]);
}
Console.WriteLine();
}
//static void ArrayPrint<T>(T[] Num)
//{
// for (int i = 0; i < Num.Length; ++i)
// {
// Console.Write("{0} ", Num[i]);
// }
// Console.WriteLine();
//}
static void Main(string[] args)
{
int[] ArrayInt1 = { 2, 4, 6, 8 };
int[] ArrayInt2 = new int[4];
ArrayIntCopy(ArrayInt2, ArrayInt1);
//ArrayPrint<int>(ArrayInt2);
double[] Arraydouble1 = { 2.1, 4.1, 6.1, 8.1 };
double[] Arraydouble2 = new double[4];
ArrayDoubleCopy(Arraydouble2, Arraydouble1);
//ArrayPrint<double>(Arraydouble2);
}
}
}
>double형 메서드를 추가했다!
-double을 Generic!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_003
{
class Program
{
static void ArrayCopy<T>(T[] Dst, T[] Src)
{
for (int i = 0; i < Src.Length; ++i)
{
Dst[i] = Src[i];
Console.Write("{0} ", Dst[i]);
}
Console.WriteLine();
}
static void ArrayPrint<T>(T[] Num)
{
for (int i = 0; i < Num.Length; ++i)
{
Console.Write("{0} ", Num[i]);
}
Console.WriteLine();
}
static void Main(string[] args)
{
int[] ArrayInt1 = { 2, 4, 6, 8 };
int[] ArrayInt2 = new int[4];
ArrayPrint<int>(ArrayInt2);
ArrayCopy<int>(ArrayInt2, ArrayInt1);
double[] Arraydouble1 = { 2.1, 4.1, 6.1, 8.1 };
double[] Arraydouble2 = new double[4];
ArrayPrint<double>(Arraydouble2);
ArrayCopy <double>(Arraydouble2,Arraydouble1);
}
}
}
*type이 여러가지 일 때의 Generic
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_005
{
class Program
{ // type이 여러가지일 경우 T1,T2 이렇게 사용한다!
static void TwoPrint <T1,T2> (T1 Arg1, T2 Arg2)
{
Console.WriteLine(Arg1);
Console.WriteLine(Arg2);
}
static void Main(string[] args)
{
TwoPrint(3, 2.1);
TwoPrint("헬로", 4.1f);
}
}
}
* Generic Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_006
{
// Class Generic
class FACT<T>
{
public T Value;
public void Print()
{
Console.WriteLine("FACT Value = {0}", Value) ;
}
}
class Program
{
static void Main(string[] args)
{
// 객체참조변수를 만들어줘야한다.
// FACT 클래스에 대한 객체를 만들어준다.
// int형
FACT<int> obj = new FACT<int>();
obj.Value = 100;
obj.Print();
// string형
FACT<string> obj1 = new FACT<string>();
obj1.Value = "스트링형";
obj1.Print();
}
}
}
-Generic class (type이 여러가지인 경우)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._18_006
{
// Class Generic
class FACT<T1,T2>
{
public T1 Value1;
public T2 Value2;
public void Print()
{
Console.WriteLine("FACT Value = {0},{1}", Value1,Value2) ;
}
}
class Program
{
static void Main(string[] args)
{
FACT<string, int> obj = new FACT<string, int>();
obj.Value1 = "스트링형";
obj.Value2 = 3;
obj.Print();
}
}
}
>T1, T2를 사용하여 여러가지 type형태를 맞춘다.
* C# 윈폼
<Form1.cs>
using System.Windows.Forms;
namespace _2020._08._18_007
{
public partial class Form1 : Form
{
bool bButton = false;
public Form1()
{
InitializeComponent();
}
private void Form1_DoubleClick(object sender, System.EventArgs e)
{
if (bButton == false)
{
FactButton.Text = "더블 클릭 당함";
bButton = true;
}
else
{
FactButton.Text = "헬로 지옥으로";
bButton = false;
}
}
private void FactButton_MouseMove(object sender, MouseEventArgs e)
{
FactButton.Text = "마우스 치워라...";
}
private void FactButton_MouseLeave(object sender, System.EventArgs e)
{
FactButton.Text = "마우스 치웠다...";
}
}
}
<Form1.Designer.cs>
namespace _2020._08._18_007
{
partial class Form1
{
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 사용 중인 모든 리소스를 정리합니다.
/// </summary>
/// <param name="disposing">관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 디자이너에서 생성한 코드
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
/// </summary>
private void InitializeComponent()
{
this.FactButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// FactButton
//
this.FactButton.Location = new System.Drawing.Point(85, 26);
this.FactButton.Name = "FactButton";
this.FactButton.Size = new System.Drawing.Size(219, 393);
this.FactButton.TabIndex = 0;
this.FactButton.Text = "button1";
this.FactButton.UseVisualStyleBackColor = true;
this.FactButton.MouseLeave += new System.EventHandler(this.FactButton_MouseLeave);
this.FactButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.FactButton_MouseMove);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.FactButton);
this.Name = "Form1";
this.Text = "코로나";
this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button FactButton;
}
}
> Form1.cs와 Designer의 상관관계를 잘 이해하고 있어야한다!
> 윈폼은 이벤트에 대해 잘 알아야한다!
-TextBox
using System.Windows.Forms;
namespace _2020._08._18_007
{
public partial class Form1 : Form
{
bool bButton = false;
public Form1()
{
InitializeComponent();
}
// form1을 더블클릭 했을 때
private void Form1_DoubleClick(object sender, System.EventArgs e)
{
if (bButton == false)
{
FactButton.Text = "더블 클릭 당함";
bButton = true;
}
else
{
FactButton.Text = "헬로 지옥으로";
bButton = false;
}
}
// FactButton에 마우스를 올릴 때
private void FactButton_MouseMove(object sender, MouseEventArgs e)
{
FactButton.Text = "마우스 치워라...";
}
// FactButton에 마우스를 치울 때
private void FactButton_MouseLeave(object sender, System.EventArgs e)
{
FactButton.Text = "마우스 치웠다...";
}
// textBox1에 KeyDown(키를 눌렀다가 땔 때)
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{ // keys.Enter는 키보드 엔터를 의미한다.
if(e.KeyCode == Keys.Enter)
{
//textBox2.Text = textBox1.Text;
textBox2.Text = "엔터눌렀음";
}
}
// FactButton을 클릭했을 때
private void FactButton_Click(object sender, System.EventArgs e)
{
//textBox2.Text = textBox1.Text;
textBox2.Text = "버튼을 눌렀음";
}
}
}
'C#' 카테고리의 다른 글
2020.08.20(목) - C# 윈폼 & C# (0) | 2020.08.20 |
---|---|
2020.08.19(수) - C# & 윈폼 (0) | 2020.08.19 |
2020.08.13(목) - HMI (0) | 2020.08.13 |
2020.07.31(금) - 라즈베리파이 & C# (0) | 2020.07.31 |
2020.07.30(목) - 라즈베리파이 & C# (0) | 2020.07.30 |