Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- C#
- python
- github
- parameter
- guide
- Tutorial
- Algorithm
- 오류
- 문제풀이
- 기초
- 파이썬
- UE5
- Material
- 시작해요 언리얼 2022
- 프로그래밍
- 백준
- dfs
- 재귀
- Unreal Engine 5
- W3Schools
- Class
- w3school
- Basic
- loop
- dynamic
- c++
- Unity
- DP
- Programming
- String
Archives
- Today
- Total
행복한 개구리
C# 21.03.23.수업내용 본문
대리자 + 익명함수(람다) + Linq // 많이 사용
SayHello 메서드와 람다식 {};(비어있는 메서드) 메서드는 서로 다른 메서드이다.
sendMessage("안녕하세요")의 "안녕하세요"는 SendMessage 메서드의 string message 매개변수에 해당하는 부분이다.
Action<string>(스트링타입)의 대리자 sendMessage는 SendMessage메서드를 불러왔기 때문에.
오른쪽은 SendMessage를 람다 문으로 변형한 코드;
값을 가진 람다 식 Func의 사용.
LINQ : 배열, 열거식클래스, XML도큐먼트, 관계형 데이터베이스 서드파티 데이터 소스로부터 데이터를 편리하게 추출하고 가공하기위해 사용한다.
LinQ 연습//람다식 '=>', '=='에서 자꾸 실수를 많이한다.
아직 익숙하지 않아서 그런지 IEnumerable이 어렵다.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2
{
public class Book {
public string ID { get; set; }
public string Title { get; set; }
public DateTime PublishDate { get; set; }
}
public class App
{
//생성자
public App() {
Console.WriteLine("App");
//컬렉션 인스턴스화
List<Book> books = new List<Book>();
//books.Find(delegate (Book book) {
// return book.ID == "bk109";
//});
var book1 = new Book()
{
ID = "bk109",
Title = "어린왕자",
PublishDate = new DateTime(2002, 01, 01)
};
Console.WriteLine("{0} {1} {2}", book1.ID, book1.Title, book1.PublishDate);
books.Add(book1);
Book foundBook = books.FindLast((book) => {
DateTime year2001 = new DateTime(2001, 01, 01);
return book.PublishDate < year2001;
});
if (foundBook != null)
{
Console.WriteLine("{0} {1} {2}", foundBook.ID, foundBook.Title, foundBook.PublishDate);
}
else
{
Console.WriteLine("책을 찾지 못했습니다.");
}
}
}
}
집에 가서 한번 더 보기.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study004
{
public class App
{
//생성자
public App()
{
Console.WriteLine("App");
string[] input =
{
"달팽이",
"주황버섯",
"슬라임"
};
List<string> monsters = new List<string>(input);
Console.WriteLine("\nCapacity: {0}", monsters.Capacity);
Console.WriteLine();
foreach(string name in monsters)
{
Console.WriteLine(name);
}
Console.WriteLine("\nAddRange(monsters)");
monsters.AddRange(monsters);
Console.WriteLine();
foreach(string name in monsters)
{
Console.WriteLine(name);
}
Console.WriteLine("\nRemoveRange(2, 2)");
monsters.RemoveRange(2, 2);
Console.WriteLine();
foreach (string name in monsters)
{
Console.WriteLine(name);
}
Console.WriteLine("\n" + input);
input = new string[]
{
"장난감 병정",
"네펜데스",
"주니어 예티"
};
Console.WriteLine("\nInsertRange(3, input)");
monsters.InsertRange(3, input);
foreach(string name in monsters)
{
Console.WriteLine(name);
}
Console.WriteLine("\noutput = mosters.GetRange(2,3).ToArray()");
string[] output = monsters.GetRange(2, 3).ToArray();
Console.WriteLine();
foreach(string name in output)
{
Console.WriteLine(name);
}
Console.WriteLine();
foreach (string name in monsters)
{
Console.WriteLine(name);
}
}
}
}
MS docs보고 예제 연습함.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study004
{
public class App
{
//생성자
public App()
{
Console.WriteLine("App");
List<string> monsters = new List<string>
{
"예티",
"페페",
"발록"
};
Display(monsters);
Console.WriteLine("\nSort with generic Comparison<string> delegate:");
monsters.Sort(CompareMopsByLength);
Display(monsters);
Console.WriteLine();
}
private int CompareMopsByLength(string x, string y)
{
if (x == null)
{
if(y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if(y == null)
{
return 1;
}
else
{
int compLength = x.Length.CompareTo(y.Length);
if (compLength != 0)
{
return compLength;
}
else
{
return x.CompareTo(y);
}
}
}
}
private void Display(List<string> list)
{
foreach (string name in list)
{
Console.WriteLine(name);
}
}
}
}
Compare 연습
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study004
{
public class App
{
//대리자 선언
public delegate void ThereIsAFire(string location);
//생성자
public App()
{
Console.WriteLine("App");
//대리자 변수 선언 및 초기화
ThereIsAFire fire = new ThereIsAFire(this.Call119);
fire += new ThereIsAFire(this.ShotOut);
fire += new ThereIsAFire(this.Escape);
/*ThereIsAFire fire = (ThereIsAFire)Delegate.Combine(new ThereIsAFire(this.Call119), new ThereIsAFire(this.ShotOut), new ThereIsAFire(this.Escape));*/
fire("우리집");
}
public void Call119(string location)
{
Console.WriteLine("{0}에 불났어요! 빨리 와주세요!", location);
}
public void ShotOut(string location)
{
Console.WriteLine("피하세요! {0}에 불났어요!", location);
}
public void Escape(string location)
{
Console.WriteLine("{0}에서 탈출합니다.", location);
}
}
}
delegate 연습
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Study004
{
public delegate void Fire(string location);
public class App
{
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
public class Counter
{
//이벤트
public EventHandler<ThresholdReachedEventArgs> ThresholdReached;
private int threshold;
private int total;
//생성자
public Counter(int passedThreshold)
{
this.threshold = passedThreshold;
}
public void Add(int x)
{
this.total += x;
if (this.total >= this.threshold)
{
//이벤트 매개 객체 생성 + 데이터 셋업
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
//대리자 선언 및 초기화
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
//이벤트 발생
handler(this, args);
}
}
}
public class Button
{
public Action onPress; //대리자 변수 선언
//생성자
public Button()
{
}
public void Press()
{
this.onPress();
}
}
//생성자
public App()
{
Console.WriteLine("App");
Button btn = new Button();
Counter counter = new Counter(10);
counter.ThresholdReached += CounterThresholdReached;
btn.onPress = () =>
{
while (true)
{
Thread.Sleep(500);
counter.Add(1);
}
};
btn.Press();
}
private void CounterThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit(0);
}
}
}
살ㄹ...주..ㅓ...
'C# > 수업내용' 카테고리의 다른 글
C# 21.03.25.수업내용 (0) | 2021.03.25 |
---|---|
C# 21.03.24.수업내용 (0) | 2021.03.24 |
C# 21.03.22.수업내용 (0) | 2021.03.22 |
C# 21.03.19.수업내용 (0) | 2021.03.19 |
C# 21.03.18.수업내용 (0) | 2021.03.18 |