Presentation is loading. Please wait.

Presentation is loading. Please wait.

.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.

Similar presentations


Presentation on theme: ".NET and .NET Core 5.2 Type Operations Pan Wuming 2016."— Presentation transcript:

1 .NET and .NET Core 5.2 Type Operations Pan Wuming 2016

2 Topics Elementary Types Casting and Type Conversions
Boxing and Unboxing Implicit Types Anonymous Types Nullable types Dynamic as and is Operators Three new languages: C#, IL and F# Evolving from OO Paradigm Everything is object Members for Abstraction Accessibility for Encapsulation Inheritance and Polymorphism Solving name confliction and multi-inheritance Towards higher order: Expressiveness Delegate Lambda Expression Generic Attribute LINQ Compiler API Effective coding References: Types (C# Programming Guide)(MSDN)

3 Types, Variables, and Values
C# is a strongly-typed language. The information stored in a type can include the following The storage space that a variable of the type requires. The maximum and minimum values that it can represent. The members (methods, fields, events, and so on) that it contains. The base type it inherits from. The location where the memory for variables will be allocated at run time. The kinds of operations that are permitted.

4 Built-in Types Integers: byte, int…
Floating point values: float, double… Boolean expressions: bool Text characters: char Decimal values: decimal String: string Object Array

5 Strings A string is an object of type String whose value is text.
Internally, the text is stored as a sequential read-only collection of Char objects. A C# string can contain any number of embedded null characters ('\0'). The Length property of a string String objects are immutable

6 Creating, Manipulating, Format, and Comparing Strings

7 Using StringBuilder for Fast String Creation
StringBuilder objects are not immutable.

8 Related Topics about String (MSND)

9 Interning of Strings A table called the intern pool that contains a single reference to each unique literal string An instance of a literal string with a particular value only exists once in the system The Intern method The IsInterned method

10 Arrays: to store multiple variables of the same type
An array can be Single-Dimensional, Multidimensional or Jagged. The number of dimensions and the length of each dimension can't be changed The default values of array elements. A jagged array is an array of arrays. Arrays are zero indexed: an array with n elements is indexed from 0 to n-1. Array elements can be of any type, including an array type. Array types are reference types derived from the abstract base type Array. To use foreach iteration on all arrays in C#.

11

12 Dynamically Increase An Array’s Size
Using The List< T> class or the ArrayList class Frequently used methods and properties in List< T> Add and AddRange Insert Count (is the counterpart of Length property in Array) ElementAt Remove and RemoveRange Sort

13 Casting and Type Conversions
Implicit conversions Explicit conversions (casts) Data loss might occur Convert from a base type to a derived type User-defined conversions Conversions with helper classes, such as the System. Convert class

14 Conversion Operators Conversions are named for the type to which they convert. Either the type of the argument or the type of the result of the conversion must be the containing type. Conversions declared as implicit occur automatically when it is required. Conversions declared as explicit require a cast to be called. All conversions must be declared as static.

15 Checked The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions. When overflow occurs an overflow exception is raise. The unchecked keyword is used to suppress overflow-checking

16 Common Type System (CTS): Unified Type Hierarchy

17 Value Types Value types derive from System.ValueType
There are two categories of value types: struct and enum Boxing and Unboxing

18 Boxing and Unboxing Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.

19 Unboxing Checking the object instance to make sure that it is a boxed value of the given value type. Copying the value from the instance into the value-type variable.

20 Implicit Types To Implicitly type a local variable by var
The compiler determines and assigns the most appropriate type to the variable

21 Anonymous Types To encapsulate a set of read-only properties into a single object without a type definition. Using the new operator together with an object initializer Anonymous types are class types that derive directly from object. Array of anonymously typed elements

22 if(x.HasValue) j = x.Value;
Nullable types Being useful when passing data to and from certain databases The syntax T? is shorthand for Nullable<T>, whereT is a value type. Assign a value to a nullable type Nullable<T>.GetValueOrDefault Use the HasValue and Value read-only properties if(x.HasValue) j = x.Value;

23 Using Type dynamic An object of type dynamic bypasses static type checking At compile time, an element that is typed as dynamic is assumed to support any operation. The dynamic language runtime (DLR) is a new API in .NET Framework 4. the dynamic type in C# the implementation of dynamic programming languages such as IronPython

24

25 Improve the experience of interoperating with COM APIs

26 Safely Cast by Using as and is Operators

27 See You Next Class Learn the whole when you are learning, then you are really learning! Xun Zi


Download ppt ".NET and .NET Core 5.2 Type Operations Pan Wuming 2016."

Similar presentations


Ads by Google