39
Introduction to C#

Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Embed Size (px)

Citation preview

Page 1: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Introduction to C#

Page 2: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Why C#?

Page 3: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Develop on the Following Platforms• ASP.NET

• Native Windows

• Windows 8 / 8.1

• Windows Phone

• WPF

• Android (Xamarin)

• iOS (Xamarin)

Page 4: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

How Windows and Windows Phone Applications are developed?

Page 5: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Tools Needed

Page 6: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Where to start?

Page 7: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

The Basics of C#

Lots of similarities with C++

• Object-Oriented

• Classes, structs, enums

• Familiar basic types: int, double, bool, …

• Familiar keywords: for, while, if, else, …

• Similar syntax: curly braces { }, dot notation, …

• Exceptions: try and catch

Page 8: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

The Basics of C#

• Everything lives in a class/struct (no globals)

• No pointers! (so no ->, * or & notation)

• Garbage collection: no delete!

• No header files

• Interfaces

• Static members accessed with . (not ::)

In a nutshell: much easier than C++

Page 9: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

C# Features

• Properties

• Interfaces

• The foreach keyword

• The readonly keyword

• Parameter modifiers: ref and out

• Delegates and events

• Generics (Instead of Templates)

Page 10: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties

• Class members, alongside methods and fields

• “field” is what C# calls a member variable

• Properties “look like fields, behave like methods”

• By convention, names are in UpperCamelCase

Page 11: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Propertiesclass Person{

// Private field (the "backing field")private String name;

// Public propertypublic String Name{

get{ return name;}

set{

// "value" is an automatic// variable inside the settername = value;

}}

}

class Program{

static void Main( string[] args ){

Person p = new Person();

// Use the setterp.Name = "Waseem";

// Use the getterConsole.WriteLine( p.Name );

}}

Page 12: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties Demo

Page 13: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties

• So far, looks like an over-complicated field

• So why bother?

Page 14: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties : Advanced Getter / Setter• Can hide implementation detail inside property

• Hence “looks like a field, behaves like a method”class Person{

// Private field (the "backing field")private String name;

private static int refCount = 0;

// Public propertypublic String Name{

get{ return name.ToUpper();}

set{

name = value;refCount++;

}}

}

Page 15: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties : Access Modifiers

• Now only the class itself can modify the value

• Any object can get the valueclass Person{

// Private field (the "backing field")private String name;

// Public propertypublic String Name{

get{ return name;}private set{ name = value;}

}}

Page 16: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties : Getter only

• In this case it doesn’t make sense to offer a setter

• Can also implement a setter but no getter

• Notice that Now and Hour are both properties too (of DateTime) –

and Now is static!class Thing{

// Public propertypublic int CurrentHour{

get{ return DateTime.Now.Hour;}

}}

Page 17: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties : Even simpler example

class Person{

// If all you want is a simple// getter / setter pair, no need for a// backing field at allpublic String Name { get; set; }

// As you might have guessed, access// modifiers can be usedpublic bool IsWorking { get; private set; }

}

Page 18: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Properties

• A really core feature of C#

• You’ll see them everywhere

• DateTime.Now

• String.Length

• Etc.

• Get into the habit of using a property whenever you need a getter

and/or setter

• Preferred to using GetValue(), SetValue() methods

• Never use public fields!

Page 19: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Interfaces

• Like a class, but all its members are implicitly abstract

• i.e., it does not provide any method implementations,

only method signatures

• A class can only inherit from a single base class, but may

implement multiple interfacesinterface ILog{ void Log( string text );}

Page 20: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Interfaces Demo

Page 21: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

foreach

• Simplified for loop syntax

• Works with built-in arrays, collection classes and any class

implementing IEnumerable interface

• String implements IEnumerable<char>

int[] myInts = new int[] { 1, 2, 3, 4, 5 };foreach ( int i in myInts ){ Console.WriteLine( i );}

Page 22: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Foreach Demo

Page 23: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Parameter Modifiers: ref

• No (explicit) pointers or reference in C#

• In effect, all parameters are passed by reference

• But not quite…static void Main( string[] args ){

String message = "I'm Waseem";Negate( message );Console.WriteLine( message );

}

static void Negate( String text ){ text += " NOT";}

> I’m Waseem

Output

Page 24: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Parameter Modifiers: ref

• Although it looks like parameter passing is “by reference”,

but it actually is “by const reference”

• The ref keyword fixes this

> I’m Waseem NOT

Outputstatic void Main( string[] args ){

String message = "I'm Waseem";Negate( ref message );Console.WriteLine( message );

}

static void Negate( ref String text ){ text += " NOT";}

Page 25: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Parameter Modifier: ref Demo

Page 26: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Parameter Modifier: out

• Like ref but must be assigned in the method

static void Main( string[] args ){

DateTime now;if ( IsAfternoon( out now ) ){

Console.WriteLine( "Good Afternoon, it is now " + now.TimeOfDay.ToString() );

}else{ Console.WriteLine( "Please come back this afternoon" );}

}static bool IsAfternoon( out DateTime currentTime ){

currentTime = DateTime.Now;return currentTime.Hour >= 12;

}

Page 27: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Parameter Modifier: out Demo

Page 28: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Delegates

• Delegates are how C# defines a dynamic interface between

two methods

• Same goal as function pointers in C

• Delegates are type-safe

• Consists of 2 parts: a delegate type and a delegate

instance

Page 29: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Delegates

• A delegate type looks like an (abstract) method declaration,

preceded with the delegate keyword

• A delegate instance creates an instance of this type,

supplying it with the name of the real method to attach to

• Example on next slide

Page 30: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Delegates// Delegate type (looks like an abstract method)delegate int Transform( int number );

// The real method we're going to attach to the delegatestatic int DoubleIt( int number ){ return number * 2;}

static void Main( string[] args ){

// Create a delegate instanceTransform transform;// Attach it to a real methodtransform = DoubleIt;// And now call it (via the delegate)int result = transform( 5 );Console.WriteLine( result );

}

I can never remember the syntax for either!Keep a handy reference book…

Page 31: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Multicast Delegates

• A delegate instance can have more than one function

attached to it

• Now when we call transform(), all methods are called

• In the order in which they were added

Transform transform = DoubleIt;transform += HalveIt;transform += TripleIt;// etc.

Page 32: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Multicast Delegates

• Methods can also be removed from a multicast delegate

transform -= HalveIt;

Page 33: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Delegates Demo

Page 34: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Events

• Events are really just special instances of delegates

• By convention they have a specific declaration for the delegate:

• We declare an event like this

• We use an event just like a delegate except that only the += and -=

can be used by classes

delegate void GuestHandler( String name );

event GuestHandler EnterEvent;

Page 35: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Events Demo

Page 36: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

using Statement

• Automatically disposes the object

• The object must implement IDisposable interface

using ( TextWriter w = File.CreateText( "log.txt" ) ){ w.WriteLine( "This is line one" );}

Page 37: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Questions?

Page 38: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

References

• Microsoft Virtual Academy

Page 39: Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Thank You