Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
Applied object orientedprogramming
Operators and type systems
Today
• .NET history
• Operators in .NET
• Loops
• Type systems
.NET history
VU, MIF, Programų sistemų katedra
VU, MIF, Programų sistemų katedra
Code "conversion"
VU, MIF, Programų sistemų katedra
.NET Core
• Cross-platform .NET development platform
• Runs on windows/macOS/linux
• Open source https://github.com/dotnet/core
• Languages that can be used with: C#, F# and VB
• Still new*, so it is changing a lot
• .NET Core 3.0 is scheduled for September 2019
* RC released late 2015, stable – mid 2016
VU, MIF, Programų sistemų katedra
.NET Core vs .NET.NET .NET Core
Cross-platform - +
Windows forms + + (only Win)
WCF services + -
WPF + + (only Win)
Lightweight with containers
- +
VU, MIF, Programų sistemų katedra
Which framework should you choose?
• Depends on your needs:• Is it important for you to have cross-platform application?
• Do you want to use containers
• Depends on available external packages:• Before choosing framework – do analysis on existing packages that you will
need to use for assignments.
• Course recommendation: .NET framework + .NET Core framework
VU, MIF, Programų sistemų katedra
Arithmetic operators
• A = 10; B = 20
VU, MIF, Programų sistemų katedra
Comparison operators
VU, MIF, Programų sistemų katedra
Logical operators
• Short-circuit evaluation: look in and.csA = True, B = False
VU, MIF, Programų sistemų katedra
VU, MIF, Programų sistemų katedra
Binary operators (1)
• A = 0011 1100
• B = 0000 1101
• -----------------
• A&B = 0000 1100
• A|B = 0011 1101
• A^B = 0011 0001
• ~A = 1100 0011
VU, MIF, Programų sistemų katedra
Binary operators (2)
• A = 60 (0011 1100), B = 13 (0000 1101)
VU, MIF, Programų sistemų katedra
Assignment operators
VU, MIF, Programų sistemų katedra
Syntax sugar• Syntax sugar
• Simple as +=
• Get/Set changed to property
• Type inference, as example.
• Syntax salt• „break“ usage inside „switch“
• When hiding inherited method, must use „new“
• Designed to prevent one from writing bad code
VU, MIF, Programų sistemų katedra
Syntactic saccharin
• Gratuitous syntax that does not make programming any sweater :)
VU, MIF, Programų sistemų katedra
var• int i = 10;
• Explicit definition
• var i = 10;• Implicit definition
• Implicit type can be:• int, bool, etc.
• Anonymous type
• User defined type
• .NET class
VU, MIF, Programų sistemų katedra
Ternary operator
• condition ? value if true : value if false
VU, MIF, Programų sistemų katedra
Operator overload
VU, MIF, Programų sistemų katedra
• Word „operator“ and the operator sign.
• Specific heading (look operatorOverload.cs)
• More on: Overloadable Operators, MSDN
public static Box operator +(Box b, Box c)
Operator Description
+, -, !, ~, ++, --, true, false Can be overloaded (unary)
+, -, *, /, %, &, |, ^, <<, >> Can be overloaded (binary)
==, !=, <, >, <=, >= Can be overloaded
&&, || Cannot do it simple way
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= Cannot do it simple way
=, ., ?:, ??, ->, =>, f(x), as, checked, unchecked,
default, delegate, is, new, sizeof, typeofCannot do it
Null-coalescing operator
Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;
Syntactic sugar:
VU, MIF, Programų sistemų katedra
switch• int and string types (before c# 8)
• Break is a must:
VU, MIF, Programų sistemų katedra
switch expressions (c# 8)
Switch expressions (c# 8) (2)
• Property pattern – switch to filter on specific property;
• Tuple pattern – switch to filter on more than one property;
• Positional pattern – using deconstruct
• More to read - https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions
Cycles: for and foreach
VU, MIF, Programų sistemų katedra
While and do-while
VU, MIF, Programų sistemų katedra
Types• Value types
• Reference types
• Pointer types
VU, MIF, Programų sistemų katedra
Demo time
Types• Ref vs value
VU, MIF, Programų sistemų katedra
Value types
• Enumerations
• Structs (all below „under the hood“ is struct):• Numeric types
• Integral types
• Floating-point types
• decimal
• bool
• User defined structs
VU, MIF, Programų sistemų katedra
Value types
VU, MIF, Programų sistemų katedra
Value types
• GetType:
VU, MIF, Programų sistemų katedra
Formating numeric typesSymbol Definition Example Result
C or c Currency Console.Write("{0:C}", 2.5);
Console.Write("{0:C}", -2.5);
$2.50
($2.50)
D or d Decimal Console.Write("{0:D5}", 25); 00025
E or e Scientific Console.Write("{0:E}",
250000);
2.500000E+005
F or f Fixed-point Console.Write("{0:F2}", 25);
Console.Write("{0:F0}", 25);
25.00
25
G or g General Console.Write("{0:G}", 2.5); 2.5
N or n Number Console.Write("{0:N}",
2500000);
2,500,000.00
X or x Hexadecimal Console.Write("{0:X}", 250);
Console.Write("{0:X}", 0xffff);
FA
FFFF
VU, MIF, Programų sistemų katedra
Type system
VU, MIF, Programų sistemų katedra
Type system
VU, MIF, Programų sistemų katedra
Enum
• Value type, an enumeration, a distinct type that consists of a set ofnamed constants called the enumerator list
• Has:• Name,
• Hidden integer type• e.g. Byte, Int32 ar UInt64,
• A set of fields
VU, MIF, Programų sistemų katedra
• Cannot:
– Have methods
– Implement interfaces
– Define properties orevents
– Be generic
Enum
VU, MIF, Programų sistemų katedra
Enum
• Hidden type – int
• Starts with 0:
• Another way:
• Look:• enum2.cs
VU, MIF, Programų sistemų katedra
Enum
VU, MIF, Programų sistemų katedra
Struct• Value type
• Defines:• Data
• Operations on data
• Possible to do:• Create an instance
• Pass them as parameters
• Save as local types
• Have it as a field inside both – reference and value types
• Struct can implement and interface.
VU, MIF, Programų sistemų katedra
Struct
• Has:• Fields
• Methods (incl. Constructor)
• Properties
• Operators
• Events
• Delegates.
VU, MIF, Programų sistemų katedra
Struct: diff from class• Value vs reference
• Struct cannot be null
• Class is to be used for larger objects.
• Classes can be inherited, structs - cannot.
• Struct does not have destructor
• Struct cannot be abstract
• In struct you can override only these:• Equals()
• GetHashCode()
• GetType()
• ToString() – look operatorOverload.cs
VU, MIF, Programų sistemų katedra
Struct: diff from class• Events in classes are automatically locked and thread-safe,
while in structs - not thread-safe.
• Struct cannot have volatile
• Sizeof does not work with classes
• Field inicialization inside:
VU, MIF, Programų sistemų katedra
Instructs compiler not to change (optimize)
assigments of variables
Struct: diff from class
• Struct cannot have not empty constructor without params
• Static constructor works only with classes, look. struct3. cs
• Equals is behaving differently. Struct4.cs
For more:
-property.cs
-book.cs
VU, MIF, Programų sistemų katedra
Literature
• Basics:
• MCSD sertification toolkit: second chapter, ahalf of the third chapter
• MSDN: Operator Overloading Tutorial
• Extra: nullable types (highly recommended!)
• http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx
• http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
• Extra-extra:
• Decimal floating point in .NET. C# in Depth. (on web).
VU, MIF, Programų sistemų katedra
Next time
• Guests – last year best team will present their application!
• Three main OOP pillars:• Encapsulation
• Inheritance
• Polymorphism
• Demo of creating a web service using .Net Core
• Guests - GIT
Questions
VU, MIF, Programų sistemų katedra