1. Trang chủ
  2. » Công Nghệ Thông Tin

02 c advanced features

43 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề C# Advanced Features
Thể loại Bài viết
Năm xuất bản 2016
Định dạng
Số trang 43
Dung lượng 534,58 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

PowerPoint Presentation C# Advanced features Feb 2016 Agenda Automatic type, property Object & Collection initializer Anonymous type, method Delegate & event, polymorphism Reference Miscellaneous Auto[.]

Trang 1

C# Advanced

features

Feb 2016

Trang 2

1 Automatic: type, property

2 Object & Collection initializer

3 Anonymous: type, method

4 Delegate & event, polymorphism

5 Reference

6 Miscellaneous

Trang 3

Automatic type & property

1

Trang 5

3 What is the benefit?

4 Why would developer want to use it?

 Actually, when to use and when not to use?

Trang 6

My best usage scenario

 List declaration

 List iteration

Trang 7

Later usage in this

course

Trang 8

Automatic property

 OOP encapsulation rule

 Never expose public fields, use property instead!

 Better for logic adding later

No breaking changes! => Better maintenance

 Properties can be databound, fields cannot!

Trang 9

Equivalent

 Whatever you write, the right code will

always be generated! (Kind of similarity, not exactly)

Trang 11

Read only property

 Lúc trước

public string FirstName { get; private set; }

public string LastName { get; private set; }

 Bản mới

public string FirstName { get; }

public string LastName { get; }

Trang 12

Object & Collection initializer

2

Trang 13

Object initializer

 Old way to initialize

 New and more convenient way to initialize

Trang 14

Collection initializer

Trang 15

Anonymous type & method

3

Trang 16

Things to consider

 Same class?

Trang 17

Anonymous type

 Usage scenario: purely for storing data

(database access)

Trang 18

Delegate & event

4

Trang 20

Delegate example

Trang 21

Event

 Like Win32 API callback

 Register function that will be called when an

event happen

Trang 22

Event example

Trang 23

Usage scenario

 Create processing framework

 Generalization

Trang 24

Lambda expression

Trang 25

Func<in T, out TResult>

Trang 26

Example – Sort method

 How to make it work with any type rather

than int?

Trang 27

Solution

Trang 28

5

Trang 29

Question 1: result?

Trang 30

Question 2: result

Trang 31

Given a Cat class

Trang 32

Question 3: result?

Trang 33

Question 4: result?

Do we still have the old cat?

Trang 34

Question 5: result?

Do we still have the old cat?

Trang 35

Weak reference

Trang 36

36

Trang 37

Extension methods

String fox = “fox”; fox.GetWordCount();

Trang 38

 Be carefull of this type of unsafe code!

 Cannot free the memory

 Automatically delete after method ends

Trang 39

Generics vs ArrayList

 Classes & Methods independent of

contained types

 List, Queue, Stack, Array…

 Can be Delegates, Lambdas, Events

 Vs ArrayList

Trang 40

yield return

yield break: stop iteration

Trang 41

Tuple

Trang 42

checked & unchecked

 checked: forced overflow checking, throw

OverflowException

Trang 43

is & as operator

 is: checking type

 as: safe type casting

Ngày đăng: 09/04/2023, 06:16

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w