Top Banner
Anonymous Methods, Partial Types and Nullable Types [email protected] Programming in C#
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 15   anonymous methods, partial types and nullable types

Anonymous Methods,

Partial Types and Nullable Types

[email protected]

Programming in C#

Page 2: 15   anonymous methods, partial types and nullable types

Anonymous Methods

Page 3: 15   anonymous methods, partial types and nullable types

Anonymous Methods an inline nameless block of code.

Page 4: 15   anonymous methods, partial types and nullable types

Features accept parameters ref, out, generic type cannot include jump statements such as goto and break

Page 5: 15   anonymous methods, partial types and nullable types

Benefits No separate named methods Reduced number of methods Better understanding

Page 6: 15   anonymous methods, partial types and nullable types

Implementation Anonymous methods are not given any return type Anonymous methods are not prefixed with access modifiers

https://gist.github.com/2586919

Page 7: 15   anonymous methods, partial types and nullable types

Referencing multiple anonymous methods https://gist.github.com/2586945

Page 8: 15   anonymous methods, partial types and nullable types

Passing parameters https://gist.github.com/2586982

Page 9: 15   anonymous methods, partial types and nullable types

Partial Types

Page 10: 15   anonymous methods, partial types and nullable types

Partial Types indicates that other parts of the class, struct or interface can be defined in different files or namespaces. All the parts will be formed to the final type at compile time.

Page 11: 15   anonymous methods, partial types and nullable types

Partial type? working on large projects, multiple programmers work on one class. working with automatically generated source, code can be added to the class without modifying the source file.

Page 12: 15   anonymous methods, partial types and nullable types

Implementation Each of the partial parts of the code must have the same access modifier

if a part of code is declared as abstract and others are declared as public => the entire code is considered abstract. Same as sealed class.

https://gist.github.com/2592542

Page 13: 15   anonymous methods, partial types and nullable types

Implementation (cont.) Partial Class

https://gist.github.com/2592720

Page 14: 15   anonymous methods, partial types and nullable types

Question

Page 15: 15   anonymous methods, partial types and nullable types

Question

Page 16: 15   anonymous methods, partial types and nullable types

Nullable Types

Page 17: 15   anonymous methods, partial types and nullable types

Nullable types? Before C# 2.0, only reference type could be null

Page 18: 15   anonymous methods, partial types and nullable types

Nullable types Null values can be defined for the value types. instance of the System.Nullable<T> struct.

Page 19: 15   anonymous methods, partial types and nullable types

Implementation Nullable types have two properties: HasValue Value can also use the == and != operators if (x != null) y = x; https://gist.github.com/2592834

Page 20: 15   anonymous methods, partial types and nullable types

Implementation (cont.) Nullable types in Expression https://gist.github.com/2592857

Page 21: 15   anonymous methods, partial types and nullable types

?? Operator assign a nullable type to a non-nullable type. https://gist.github.com/2592875