SlideShare a Scribd company logo
1 of 80
Download to read offline
C# 
All the awesomeness you need for SSA. 
igrali@outlook.com 
twitter.com/igrali 
igrali.com 
dizzy.hr 
Igor Ralić
C# basics 
OOP in C# 
Advanced C#
Why do Java developers wear glasses? 
-Because they don't C#!
C# 
Simple, object-oriented, managed, type-safe 
Familiar to C, C++, Java (and more) devs 
Developed by Microsoft / Anders Hejlsberg 
C# 5 
Standardized – Ecma, ISO
Managed? 
Depends on services provided by runtime environment 
Common Language Runtime (CLR) executes managed code 
CLR features 
memory management 
exception handling 
standard types 
...
.NET Framework 
Microsoft developer platform 
C# works with .NET  
CLR 
Common 
Language 
Runtime 
.NET 
Framework 
Class 
Library 
Language independence
2002 
.Net 1 
C# 1 
2003 
.Net 1.1 
C# 1.2 
2005 
.Net 2 
C# 2 
GENERICS 
PARTIAL CLASSES 
ANONYMOUS 
NULLABLE 
2006 
.Net 3 
N/A 
2007 
.Net 3.5 
C# 3 
VAR 
LINQ 
LAMBDA 
INITIALIZERS 
AUTO PROPS 
EXTENSIONS 
PARTIAL METHODS 
2010 
.Net 4 
C# 4 
DYNAMICS 
OPTIONAL ARGS 
COVARIANCE 
2012 
.Net 4.5 
C# 5 
ASYNC 
CALLER ATTR
Our second program
Layout matters!
Identifiers and keywords 
Identifiers 
Program, Main, x, y … 
Start with letter or underscore, case-sensitive 
Keywords 
using, class, static, void 
Can’t use as identifiers
Storing numbers 
Integers 
Real numbers 
float, double, decimal 
Type Approximate range Precision 
float -3.4 × 10 
38 
to +3.4 × 10 
38 
7 digits 
−324 
double ±5.0 × 10 
308 
to ±1.7 × 10 
15-16 digits 
decimal (-7.9 x 10 
28 
to 7.9 x 10 
28 
) / (10 
0 to 28 
) 28-29 significant digits
Storing text and state 
char 
string 
Immutable 
StringBuilder is mutable 
Store state – bool 
true - false
Arrays 
Fixed number of elements of type 
Contiguous block of memory 
Highly efficient access 
Indexer starting at 0
Controlling program flow 
if – else if – else 
can be nested 
Break down conditions
Switch statement 
Branch based on a selection of 
possible value
Loops 
Repeat something a certain number of times 
do – while 
while 
for 
break 
continue
foreach 
Iterate over each element in an enumerable object 
Array, Collection, List<T>
Conversions 
Implicit 
Automatic 
Only when no information will be lost 
Explicit 
Require a cast
Types 
Blueprints for values 
Bunch of predefined types 
More in .NET framework 
System.DateTime 
Custom types
Predefined value & reference types 
Value types 
Signed integers – sbyte, short, int, long 
Unsigned integers – byte, ushort, uint, ulong 
Real numbers – float, double, decimal 
Logical – bool 
Character – char 
Reference types 
String, object
Value vs. reference types 
Value types 
numeric types, char, bool 
struct, enum types 
Reference types 
class, array, delegate 
Null reference 
Image source: C# 5.0 Pocket Reference by Joseph Albahari and Ben Albahari
Value vs. reference types
Nullable type 
Null useful to represent nonexistent value 
What about value types? 
tip + ?
Instance vs. Static 
Instance members operate on instances  
Static operate on the type itself
Method parameters 
Parameter 
modifier 
Passed by 
Variable must 
be assigned 
none Value Going in 
ref Reference Going in 
out Reference Going out
Method parameters 
Params 
Optional parameters
var – implicitly typed variables 
Compiler can (most of the time) infer the type
Object-oriented
OOP in C# 
Inheritance 
Inherit members from parent class 
Reuse, extend or modify behavior defined in other classes 
Encapsulation 
Hide internals of a class 
Polymorphism 
Subytpe polymorphism 
Take different shapes
Classes 
Most common kind of reference type 
Blueprint for objects 
Object = instance of a class 
Reference type 
Unlike structs 
Supports inheritance 
Unlike structs
Structs 
Cannot initialize fields in declaration 
Unless const or static 
Cannot declare parameterless constructor 
Value types 
Cannot inherit from another struct or class 
Can be instantiated without using new operator
Access modifiers 
Promotes encapsulation 
public 
Fully accessible, implicit for enum and interface members 
internal 
Accessible within the containing assembly 
private 
Accessible only within containing type, implicit for class/struct members 
protected 
Accessible only within the containing type and subclasses
Basic elements 
Fields 
Variable member of class or struct 
Can be readonly – cannot be modified after construction 
Initialization is optional – default value 
Initializers run before constructors
Basic elements 
Methods 
Perform an action 
Can take input from the caller 
Can return output 
Can be overloaded
Basic elements 
Instance constructors 
Run initialization code 
Almost like a method – no return type, same name as type name 
Can be overloaded 
One constructor can call another one – the called one gets executed first 
Implicit parameterless constructor 
Until you define at least one constructor 
Don’t need to be public
Basic elements 
Properties 
Similar to public fields, with get/set logic 
get – read 
set – assign 
Read-only if it has just a getter 
Write-only if it has just a setter (very rare) 
Can change accessibility 
Automatic properties
Indexers 
Access to inner 
list/dictionary of values
Inheritance 
Derived class extends base class 
A class can inherit from a single class 
Only multiple interfaces can be implemented 
A class can be inherited by many classes 
Animal 
Dog Frog Cat
Constructors & inheritance 
Subclass must declare its own constructor 
Can use keyword base to access base class’s constructors 
Base class constructors always execute first
Polymorphism 
Two aspects 
Objects of a derived class may be treated as objects of a base class in 
places such as method parameters 
Base classes may define and implement virtual methods, and derived 
classes can override them
Virtual members 
Methods, properties, indexer and events 
Function marked as virtual can be overridden 
With specialized implementation
Virtual members
Virtual members
Abstract classes and members 
Abstract class cannot be instantiated 
Abstract classes can define abstract members 
Like virtual, except they don’t provide default implementation 
Derived classes of the abstract class must implement all 
abstract methods.
Abstract classes and members
Sealing functions and classes 
Overriden function member may seal its implementation 
keyword sealed
System.Object 
Base class for all types 
Any type can be implicitly upcast to object
Boxing and unboxing 
Boxing 
Cast a value-type instance to a reference-type instance 
Unboxing 
Reverse – cast a reference-type instance to a value-type instance 
Computationally expensive – best if avoided – see generics!
Is & As 
is operator returns true if an object is an instance of a type 
as attempts to cast to a specified type 
null if not possible 
does not raise an exception
Interfaces 
Specification – no implementation! 
Contract 
All members implicitly abstract 
Class/struct can implement multiple interfaces 
Reminder: class can only inherit from one class! 
May extend other interfaces
Interfaces 
Windows Phone Silverlight 
DialogService : IDialogService 
with MessageBox 
Windows 8.1 WinRT 
DialogService : IDialogService 
with MessageDialog
Enums 
Useful when storing state information 
Whenever you think of something in terms of state 
Comparing enum values is much smarter than comparing hardcoded 
strings
Advanced C# concepts
Generics 
Write code that’s reusable across types – type parameters 
Inheritance & generics 
Increase type safety 
Reduce casting and boxing/unboxing 
Supply type 
arguments 
T 
Very bad 
implementation 

Generics
Generics 
Most of the data structures and collection implemented this 
way 
Lists, dictionary, stack, queue, observable collection ...
Generic methods 
Methods can be generic, too!
Generic constraints 
Constraint Description 
where T: base-class Base-class constraint 
where T: interface Interface constraint 
where T: class Reference-type constraint 
where T: struct Value-type constraint 
where T: new() Parameterless constructor constraint 
where U: T Naked type constraint
Delegates 
A type-safe reference to a method in a class 
Very useful when we want to pass a method to somewhere else so it 
can execute it when needed
Action & Func 
Action 
Do something with certain parameters, return nothing 
Func 
Do something with certain parameters, return something
Action & Func
Events 
Publish – subscribe 
+= subscriber starts listening 
-= subscriber stops listening 
Class allows others to give 
it delegates 
Those delegates then get 
invoked when event occurs
Anonymous functions 
In C# 2.0, lambdas used nowadays
Lambda expressions (not lamba  ) 
Unnamed method in place of delegate instance
Lambdas 
(parameters) => expression-or-statement-block 
(x) => x*x; read: x goes to
Lambdas 
Possible to capture outer variables – a closure 
captured variables
Lambdas 
What’s the output?
Exceptions 
Bad things happen 
try-catch-finally 
catch Exception or a subclass of Exception 
try combined with catch block(s), finally block or both
Exceptions 
Catch multiple 
exceptions, 
cleanup resources 
after 
StackTrace 
Message 
InnerException
Extension methods 
Extend existing type with new methods 
Static method of static class
Async 
Better asynchronous model in C# 5.0 
async & await 
return to caller instead of blocking it 
concurrency 
multithreading 
callback
Async await 
Almost as 
simple as 
sync 
calls
Async lambdas 
Both named and unnamed methods can be async 
Can be useful with event handlers
And so much more...
Sources 
MSDN documentation 
http://msdn.microsoft.com/en-us/library/618ayhy6.aspx 
C# Yellow Book – Rob Miles 
http://www.robmiles.com/c-yellow-book/ 
C# 5.0 Pocket Reference 
Joseph Albahari and Ben Albahari (O’Reilly)
More good resources 
Programming in C# - Jump Start – MVA 
http://www.microsoftvirtualacademy.com/training-courses/developer-training- 
with-programming-in-c 
.NET Book Zero 
http://www.charlespetzold.com/dotnet/ 
Pluralsight courses 
https://pluralsight.com/training/offers/?cc=dreamspark
C# 
All the awesomeness you need for SSA. 
igrali@outlook.com 
twitter.com/igrali 
igrali.com 
dizzy.hr 
Igor Ralić

More Related Content

What's hot

Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in RubyVolodymyr Byno
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script PatternsAllan Huang
 
Standards For Java Coding
Standards For Java CodingStandards For Java Coding
Standards For Java CodingRahul Bhutkar
 
Short notes of oop with java
Short notes of oop with javaShort notes of oop with java
Short notes of oop with javaMohamed Fathy
 
Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dartImran Qasim
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practicesmh_azad
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsDirecti Group
 
Local variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesLocal variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesSohanur63
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageHossam Ghareeb
 
Java classes in karve nagar pune
Java classes in karve nagar puneJava classes in karve nagar pune
Java classes in karve nagar puneletsleadsand
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 

What's hot (15)

Coding standards
Coding standardsCoding standards
Coding standards
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
C sharp
C sharpC sharp
C sharp
 
Java Script Patterns
Java Script PatternsJava Script Patterns
Java Script Patterns
 
Presentation on java
Presentation  on  javaPresentation  on  java
Presentation on java
 
Standards For Java Coding
Standards For Java CodingStandards For Java Coding
Standards For Java Coding
 
Short notes of oop with java
Short notes of oop with javaShort notes of oop with java
Short notes of oop with java
 
Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dart
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
Local variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesLocal variables Instance variables Class/static variables
Local variables Instance variables Class/static variables
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Java classes in karve nagar pune
Java classes in karve nagar puneJava classes in karve nagar pune
Java classes in karve nagar pune
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 

Similar to C# - Igor Ralić

Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design GuidelinesMohamed Meligy
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Featurestechfreak
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Pythondn
 
Advanced c#
Advanced c#Advanced c#
Advanced c#saranuru
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Groupbrada
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsSaurabh Narula
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questionsnicolbiden
 

Similar to C# - Igor Ralić (20)

Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design Guidelines
 
Java basics
Java basicsJava basics
Java basics
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
VB.net
VB.netVB.net
VB.net
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
Csci360 20 (1)
Csci360 20 (1)Csci360 20 (1)
Csci360 20 (1)
 
Csci360 20
Csci360 20Csci360 20
Csci360 20
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Code Metrics
Code MetricsCode Metrics
Code Metrics
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
TypeScript 101
TypeScript 101TypeScript 101
TypeScript 101
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questions
 

More from Software StartUp Academy Osijek (14)

ASP.NET - Ivan Marković
ASP.NET - Ivan MarkovićASP.NET - Ivan Marković
ASP.NET - Ivan Marković
 
XAML and WPF - Dinko Jakovljević
XAML and WPF - Dinko JakovljevićXAML and WPF - Dinko Jakovljević
XAML and WPF - Dinko Jakovljević
 
Internet marketing - Damir Podhorski
Internet marketing - Damir PodhorskiInternet marketing - Damir Podhorski
Internet marketing - Damir Podhorski
 
Team management - Tomislav Bilić
Team management - Tomislav BilićTeam management - Tomislav Bilić
Team management - Tomislav Bilić
 
ORM - Ivan Marković
ORM - Ivan MarkovićORM - Ivan Marković
ORM - Ivan Marković
 
Baze podataka i SQL - Vlatko Vlahek
Baze podataka i SQL - Vlatko VlahekBaze podataka i SQL - Vlatko Vlahek
Baze podataka i SQL - Vlatko Vlahek
 
Services - Leo Tot
Services - Leo TotServices - Leo Tot
Services - Leo Tot
 
Wireframing & UI design - Andrej Mlinarevic
Wireframing & UI design - Andrej MlinarevicWireframing & UI design - Andrej Mlinarevic
Wireframing & UI design - Andrej Mlinarevic
 
Financijski plan - Ana Marija Delic
Financijski plan - Ana Marija DelicFinancijski plan - Ana Marija Delic
Financijski plan - Ana Marija Delic
 
Izvori financiranja - Nina Marković
Izvori financiranja - Nina MarkovićIzvori financiranja - Nina Marković
Izvori financiranja - Nina Marković
 
Software Product Development - Denis Susac
Software Product Development - Denis SusacSoftware Product Development - Denis Susac
Software Product Development - Denis Susac
 
Poslovni plan - Sunčica Oberman Peterka
Poslovni plan - Sunčica Oberman PeterkaPoslovni plan - Sunčica Oberman Peterka
Poslovni plan - Sunčica Oberman Peterka
 
PM, Scrum and TFS - Ivan Marković
PM, Scrum and TFS - Ivan MarkovićPM, Scrum and TFS - Ivan Marković
PM, Scrum and TFS - Ivan Marković
 
Uvod u aplikacije - Luka Mandić
Uvod u aplikacije - Luka MandićUvod u aplikacije - Luka Mandić
Uvod u aplikacije - Luka Mandić
 

Recently uploaded

Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressMaria Paula Aroca
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Osopher
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Jason Potel In Media Res Media Component
Jason Potel In Media Res Media ComponentJason Potel In Media Res Media Component
Jason Potel In Media Res Media ComponentInMediaRes1
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Paul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media ComponentPaul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media ComponentInMediaRes1
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian Congress
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Jason Potel In Media Res Media Component
Jason Potel In Media Res Media ComponentJason Potel In Media Res Media Component
Jason Potel In Media Res Media Component
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Teaching Critical AI Literacies - Maha Bali
Teaching Critical AI Literacies - Maha BaliTeaching Critical AI Literacies - Maha Bali
Teaching Critical AI Literacies - Maha Bali
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Paul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media ComponentPaul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media Component
 
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 8 - CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC ...
 

C# - Igor Ralić

  • 1. C# All the awesomeness you need for SSA. igrali@outlook.com twitter.com/igrali igrali.com dizzy.hr Igor Ralić
  • 2. C# basics OOP in C# Advanced C#
  • 3. Why do Java developers wear glasses? -Because they don't C#!
  • 4. C# Simple, object-oriented, managed, type-safe Familiar to C, C++, Java (and more) devs Developed by Microsoft / Anders Hejlsberg C# 5 Standardized – Ecma, ISO
  • 5. Managed? Depends on services provided by runtime environment Common Language Runtime (CLR) executes managed code CLR features memory management exception handling standard types ...
  • 6. .NET Framework Microsoft developer platform C# works with .NET  CLR Common Language Runtime .NET Framework Class Library Language independence
  • 7. 2002 .Net 1 C# 1 2003 .Net 1.1 C# 1.2 2005 .Net 2 C# 2 GENERICS PARTIAL CLASSES ANONYMOUS NULLABLE 2006 .Net 3 N/A 2007 .Net 3.5 C# 3 VAR LINQ LAMBDA INITIALIZERS AUTO PROPS EXTENSIONS PARTIAL METHODS 2010 .Net 4 C# 4 DYNAMICS OPTIONAL ARGS COVARIANCE 2012 .Net 4.5 C# 5 ASYNC CALLER ATTR
  • 10. Identifiers and keywords Identifiers Program, Main, x, y … Start with letter or underscore, case-sensitive Keywords using, class, static, void Can’t use as identifiers
  • 11. Storing numbers Integers Real numbers float, double, decimal Type Approximate range Precision float -3.4 × 10 38 to +3.4 × 10 38 7 digits −324 double ±5.0 × 10 308 to ±1.7 × 10 15-16 digits decimal (-7.9 x 10 28 to 7.9 x 10 28 ) / (10 0 to 28 ) 28-29 significant digits
  • 12. Storing text and state char string Immutable StringBuilder is mutable Store state – bool true - false
  • 13. Arrays Fixed number of elements of type Contiguous block of memory Highly efficient access Indexer starting at 0
  • 14. Controlling program flow if – else if – else can be nested Break down conditions
  • 15. Switch statement Branch based on a selection of possible value
  • 16. Loops Repeat something a certain number of times do – while while for break continue
  • 17. foreach Iterate over each element in an enumerable object Array, Collection, List<T>
  • 18. Conversions Implicit Automatic Only when no information will be lost Explicit Require a cast
  • 19. Types Blueprints for values Bunch of predefined types More in .NET framework System.DateTime Custom types
  • 20. Predefined value & reference types Value types Signed integers – sbyte, short, int, long Unsigned integers – byte, ushort, uint, ulong Real numbers – float, double, decimal Logical – bool Character – char Reference types String, object
  • 21. Value vs. reference types Value types numeric types, char, bool struct, enum types Reference types class, array, delegate Null reference Image source: C# 5.0 Pocket Reference by Joseph Albahari and Ben Albahari
  • 23. Nullable type Null useful to represent nonexistent value What about value types? tip + ?
  • 24. Instance vs. Static Instance members operate on instances  Static operate on the type itself
  • 25. Method parameters Parameter modifier Passed by Variable must be assigned none Value Going in ref Reference Going in out Reference Going out
  • 26. Method parameters Params Optional parameters
  • 27. var – implicitly typed variables Compiler can (most of the time) infer the type
  • 29. OOP in C# Inheritance Inherit members from parent class Reuse, extend or modify behavior defined in other classes Encapsulation Hide internals of a class Polymorphism Subytpe polymorphism Take different shapes
  • 30. Classes Most common kind of reference type Blueprint for objects Object = instance of a class Reference type Unlike structs Supports inheritance Unlike structs
  • 31. Structs Cannot initialize fields in declaration Unless const or static Cannot declare parameterless constructor Value types Cannot inherit from another struct or class Can be instantiated without using new operator
  • 32. Access modifiers Promotes encapsulation public Fully accessible, implicit for enum and interface members internal Accessible within the containing assembly private Accessible only within containing type, implicit for class/struct members protected Accessible only within the containing type and subclasses
  • 33. Basic elements Fields Variable member of class or struct Can be readonly – cannot be modified after construction Initialization is optional – default value Initializers run before constructors
  • 34. Basic elements Methods Perform an action Can take input from the caller Can return output Can be overloaded
  • 35. Basic elements Instance constructors Run initialization code Almost like a method – no return type, same name as type name Can be overloaded One constructor can call another one – the called one gets executed first Implicit parameterless constructor Until you define at least one constructor Don’t need to be public
  • 36. Basic elements Properties Similar to public fields, with get/set logic get – read set – assign Read-only if it has just a getter Write-only if it has just a setter (very rare) Can change accessibility Automatic properties
  • 37. Indexers Access to inner list/dictionary of values
  • 38. Inheritance Derived class extends base class A class can inherit from a single class Only multiple interfaces can be implemented A class can be inherited by many classes Animal Dog Frog Cat
  • 39.
  • 40. Constructors & inheritance Subclass must declare its own constructor Can use keyword base to access base class’s constructors Base class constructors always execute first
  • 41. Polymorphism Two aspects Objects of a derived class may be treated as objects of a base class in places such as method parameters Base classes may define and implement virtual methods, and derived classes can override them
  • 42. Virtual members Methods, properties, indexer and events Function marked as virtual can be overridden With specialized implementation
  • 45. Abstract classes and members Abstract class cannot be instantiated Abstract classes can define abstract members Like virtual, except they don’t provide default implementation Derived classes of the abstract class must implement all abstract methods.
  • 47. Sealing functions and classes Overriden function member may seal its implementation keyword sealed
  • 48. System.Object Base class for all types Any type can be implicitly upcast to object
  • 49. Boxing and unboxing Boxing Cast a value-type instance to a reference-type instance Unboxing Reverse – cast a reference-type instance to a value-type instance Computationally expensive – best if avoided – see generics!
  • 50. Is & As is operator returns true if an object is an instance of a type as attempts to cast to a specified type null if not possible does not raise an exception
  • 51. Interfaces Specification – no implementation! Contract All members implicitly abstract Class/struct can implement multiple interfaces Reminder: class can only inherit from one class! May extend other interfaces
  • 52. Interfaces Windows Phone Silverlight DialogService : IDialogService with MessageBox Windows 8.1 WinRT DialogService : IDialogService with MessageDialog
  • 53. Enums Useful when storing state information Whenever you think of something in terms of state Comparing enum values is much smarter than comparing hardcoded strings
  • 55. Generics Write code that’s reusable across types – type parameters Inheritance & generics Increase type safety Reduce casting and boxing/unboxing Supply type arguments T Very bad implementation 
  • 57. Generics Most of the data structures and collection implemented this way Lists, dictionary, stack, queue, observable collection ...
  • 58. Generic methods Methods can be generic, too!
  • 59. Generic constraints Constraint Description where T: base-class Base-class constraint where T: interface Interface constraint where T: class Reference-type constraint where T: struct Value-type constraint where T: new() Parameterless constructor constraint where U: T Naked type constraint
  • 60. Delegates A type-safe reference to a method in a class Very useful when we want to pass a method to somewhere else so it can execute it when needed
  • 61. Action & Func Action Do something with certain parameters, return nothing Func Do something with certain parameters, return something
  • 63. Events Publish – subscribe += subscriber starts listening -= subscriber stops listening Class allows others to give it delegates Those delegates then get invoked when event occurs
  • 64.
  • 65.
  • 66. Anonymous functions In C# 2.0, lambdas used nowadays
  • 67. Lambda expressions (not lamba  ) Unnamed method in place of delegate instance
  • 68. Lambdas (parameters) => expression-or-statement-block (x) => x*x; read: x goes to
  • 69. Lambdas Possible to capture outer variables – a closure captured variables
  • 71. Exceptions Bad things happen try-catch-finally catch Exception or a subclass of Exception try combined with catch block(s), finally block or both
  • 72. Exceptions Catch multiple exceptions, cleanup resources after StackTrace Message InnerException
  • 73. Extension methods Extend existing type with new methods Static method of static class
  • 74. Async Better asynchronous model in C# 5.0 async & await return to caller instead of blocking it concurrency multithreading callback
  • 75. Async await Almost as simple as sync calls
  • 76. Async lambdas Both named and unnamed methods can be async Can be useful with event handlers
  • 77. And so much more...
  • 78. Sources MSDN documentation http://msdn.microsoft.com/en-us/library/618ayhy6.aspx C# Yellow Book – Rob Miles http://www.robmiles.com/c-yellow-book/ C# 5.0 Pocket Reference Joseph Albahari and Ben Albahari (O’Reilly)
  • 79. More good resources Programming in C# - Jump Start – MVA http://www.microsoftvirtualacademy.com/training-courses/developer-training- with-programming-in-c .NET Book Zero http://www.charlespetzold.com/dotnet/ Pluralsight courses https://pluralsight.com/training/offers/?cc=dreamspark
  • 80. C# All the awesomeness you need for SSA. igrali@outlook.com twitter.com/igrali igrali.com dizzy.hr Igor Ralić