15 anonymous methods, partial types and nullable types

Post on 10-May-2015

814 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Anonymous Methods,

Partial Types and Nullable Types

tnngo2@gmail.com

Programming in C#

Anonymous Methods

Anonymous Methods an inline nameless block of code.

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

Benefits No separate named methods Reduced number of methods Better understanding

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

https://gist.github.com/2586919

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

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

Partial 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.

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.

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

Implementation (cont.) Partial Class

https://gist.github.com/2592720

Question

Question

Nullable Types

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

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

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

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

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

top related