+ All Categories
Home > Software > Lecture 3 __c_sharp

Lecture 3 __c_sharp

Date post: 21-Mar-2017
Category:
Upload: mahpara-saaleem
View: 86 times
Download: 1 times
Share this document with a friend
45
C# Language CSC 313 Visual Programming Fall 2016 Zohaib Hassan Visiting Lecturer [email protected] Department of Computer Science Bahria University, Islamabad 3 LECTURE
Transcript
Page 1: Lecture 3 __c_sharp

C# LanguageCSC 313Visual ProgrammingFall 2016

Zohaib Hassan

Visiting Lecturer

[email protected]

Department of Computer Science

Bahria University, Islamabad

3LECTURE

Page 2: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :2

Zohaib Hassan | Department of Computer Science Bahria University, IslamabadPage

“The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.”

— Stephen Hawking

Page 3: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :3

Zohaib Hassan | Department of Computer Science Bahria University, IslamabadPage

History of C#

Page 4: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :4Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Versioning History

Version Date .NET Framework Visual StudioC# 1.0 Jan-02 .NET Framework 1.0 Visual Studio .NET 2002C# 1.2 Apr-03 .NET Framework 1.1 Visual Studio .NET 2003C# 2.0 Nov-05 .NET Framework 2.0 Visual Studio 2005C# 3.0 Nov-07 .NET Framework 2.0/3.0/3.5 Visual Studio 2008/2010C# 4.0 Apr-10 .NET Framework 4 Visual Studio 2010C# 5.0 Aug-12 .NET Framework 4.5 Visual Studio 2012/2013C# 6.0 Jul-15 .NET Framework 4.6 Visual Studio 2015

Page 5: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :5Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

C# 2.0• Generics• Partial types• Anonymous methods• Iterators• Nullable types• Getter/Setter separate accessibility• Static classes• And more..

Page 6: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :6Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

C# 3.0• Implicitly typed local variables• Object and collection initializers• Auto-properties• Anonymous types• Extension methods• Query expressions• Lambda expressions• Expression trees• And more..

Page 7: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :7Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

C# 4.0• Dynamic binding• Named and optional parameters• Generic co- and contravariance• And more..

Page 8: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :8Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

C# 5.0• Asynchronous methods• Caller info attributes

Page 9: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :9Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

The state of the Compiler• It’s been a black box

– .NET Compiler Platform– Microsoft decides to re-write the compiler– Compiler written in C#– Easier to extend and maintain– Open Source!

Page 10: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :10Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

C# 6.0 Overview• .NET Compiler Platform ("Roslyn")• Auto-property initializers

– Getter-only auto-properties– Assignment to getter-only auto-properties

from constructor • Parameter-less struct constructors• Using Statements for Static Members• Dictionary Initializer• Await in catch/finally• Exception filters• Expression-bodied members• Null propagation• String interpolation• Name of operator• And more..

Page 11: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :11Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

C# 7.0 (Proposal)• Local functions• Tuples• Pattern matching• Records / algebraic data types• Nullability tracking• Async streams and disposal• Strongly typed access to wire formats

Page 12: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :12Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

C# Books

Page 14: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :14Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

object• The object type is an alias for Object in the .NET Framework. • All types, predefined and user-defined, reference types and

value types, inherit directly or indirectly from Object. • can assign values of any type to variables of type object.

Page 15: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :15Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Namespace and “using”• Namespaces provide a way of organizing related classes and other types.

A name space is a logical collection rather than a physical grouping.• A name space may contain N classes, structures and enumerations etc.• Name spaces can be nested inside each other.• DOT operator can be used to access a class defined inside a name space.

Suppose we want to call public static method writeline that belongs to a class known as Console.

• The class Console belongs to name space system then we can do that as follows System.Console.WriteLine(“Hello World”);

Using Directive• A namespace that we want to access mayor may not belong to the same assembly.• “using” directive helps program torecognize a name space.

Page 16: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :16Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Namespace Exampleusing System;class Test{ public static void Main(string[] args) { Console.WriteLine("Hello World"); Hello(); Pakistan.Federal.Hello(); Pakistan.Punjab.Lahore.Hello(); } static void Hello() { System.Console.WriteLine(“Directly Called"); }}namespace Pakistan{ class Federal { public static void Hello() { System.Console.WriteLine("Hello Prime Minister!"); }}

//NESTED NAME SPACE or NAMESPACE HIERARCHYnamespace Punjab{ class Lahore { public static void Hello() { System.Console.WriteLine("Kee Haal Paa Jee!"); } } class Rawalpindi { public static void Hello() { System.Console.WriteLine("Kay hall e Phapay!"); } }}

Page 17: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :17

Zohaib Hassan | Department of Computer Science Bahria University, IslamabadPage

Using Statements for Static Members

Page 18: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :18Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Using Statements for Static Members in a nutshell

class Program{ static void Main(string[] args) { var angle = 90d; Console.WriteLine(Math.Sin(angle)); }}

Page 19: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :19Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Using Statements for Static Members in a nutshell

using System.Console;using System.Math;

class Program{ static void Main(string[] args) { var angle = 90d; WriteLine(Sin(angle)); }}

Page 20: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :20Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Page 21: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :21Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Types of Data types • All data types in C# are CTS compliant.• There are two types of data types in C#• Value type (Stored on stack Memory, limited and fast access)• Common Data types, structures• Reference Type (Stored on heap, slow access and observed by

GC)• Classes, Arrays• There are two ways to declare or define value type variables in

C#.Examples: Primitive Data TypesSystem.Int32 x=0; //CTS compliantint x1 = 0; //Using Alias, like c++int p = new int(); //Another valid way, Initialized to 0

Page 22: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :22Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

CTS Types• Data types in .NET are specified by CTS. Language datatype maps to CTS datatype.

For example, int of C# maps to System.Int32 of CTS.

• CTS have two categories of types: Value Type and Reference Type

• Every type derived from System.Object. (Directly of Indirectly)

• Data types are classes or structs that supports certain methods.

For example, string s = i.ToString();

• C# has 16 predefined types– 13 value types and

– 3 reference types (string, dynamic, object) .

CTS Types

Value Type

Reference Type

System.Object

System.ValueType

Value Types

Reference Type

Page 23: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :23Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

25a

67436b

3.14c

343.54d

-e

nullf

Stack Heap

45600 Strings are cool!

45600

g 25

int a; //32 bitslong b; //64 bits float c; //32 bits double d; //64 bits char e; //16 bits string f;int g;string h;

a = 25;b = 6743674654783474;c = 3.14F;d = 4443534534.54;e = '-';f = "Strings are cool!";g = a; //Copies actual valueh = f; //Copies reference of object

...

...

h 45600

www.dotnetvideotutorial.com

Page 24: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :24Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Value Type• Value types store value on stack• All value types are derived implicitly from the System.ValueType.• Assigning one value type variable to another copies the actual value.• While passing to function value types by default passes parameters by value.• 13 built-in value types are available (listed on next slide)• User defined value type can be created using keywords:

– struct and– enum

System.Object

System.ValueType

Value Types

Page 25: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :25Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Name CTS Type Description

sbyte System.SByte 8 bit signed integer

short System.Int16 16 bit signed integer

int System.Int32 32 bit signed integer

long System.Int64 64 bit signed integer

byte System.Byte 8 bit unsigned integer

ushort System.UInt16 16 bit unsigned integer

Uint System.UInt32 32 bit unsigned integer

ulong System.UInt64 64 bit unsigned integer

float System.Single 32 bit single precision floating point

double System.Double 64 bit single precision floating point

decimal System.Decimal 128 bit high precision decimal notation

bool System.Boolean Represents true of false

char System.Char 16 bit character (Unicode)

Page 26: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :26Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Value Data types List

Page 27: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :27Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Page 28: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :28Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Reference Type• Variables of reference types store actual data on heap and

references to the actual data on stack.• The actual data stored on heap is referred as object.• Assignment operation copies reference.• While passing to function reference types by default passes

parameters by reference.• Three built in reference types are available: string, dynamic

and object• User defined reference type can be created using keywords:

– class – Interface – delegate

Page 29: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :29Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Operators in C#

Page 30: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :30Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Assignment in C#

Page 31: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :31Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Assignment in C#• sizeof• Works just like C/C++ to find out the size of passed parameter or data type

in bytes.• typeof• “typeof” helps in checking the type of the class at the runtime.• Returns object of Type class that holds a lot of information about type in

question Look at members of Type class*

Page 32: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :32Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Structures in C#• Structures can group program primitives like classes.• C# structures are value types and are created on stack • Members of structure are private in C# unlike c++ where they are public.• Structures doesn’t support inheritance, they are implicitly sealed • Stack operations are faster than heap operations• structures are some times used as light weight classes for faster

manipulation. • structures should be used wisely because they consume limited stack

space.• All structures have Object as a base class and they can over-ride methods

of base class.• No Member function of a structure can be virtual.• Destructor and Finalize method can not be written for structures because

they are value types and have nothing to do with managed heap.

Page 33: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :33Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Structures in C#

Page 34: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :34Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Enumerations in C#• An enumeration is a user defined integer type. When we declare

enumeration, we define a set of user defined values that are considered as a domain for enumeration variable.

• One can use enumerations in function parameter or as a property of a class.• Enumerations are there in C/C++ and other languages• Enums help us to define a set of valid responses• Can be passed in functions

Page 35: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :35Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Literals in C#

Page 36: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :36Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Strings: System.String

• Note: String is a reference type

Page 37: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :37Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Variable Declarations in C#

Page 38: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :38Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

• Conversion from value type to reference type is known as boxing.• Conversion from reference type to value type is un-boxing.• A value can only be unboxed to the same data type only.

• By definition it is a process of storing value types on heap.• When we define a new value type using structures, the .NET framework

implicitly supplies another class corresponding to value class that helps us storing value types as reference types on heap using boxing.

• Common data types and enumerations also have corresponding classes.

Box and Unbox

Page 39: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :39Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

null

static void Main(string[] args) { int a = 25; object o; o = a; //boxing Console.WriteLine("o = {0}", o.ToString());

int b; b = (int)o; //unboxing Console.WriteLine("b = {0}", b.ToString());

Console.ReadKey(); }

25a

67436o

0b

25

Stack Heap

67436

Boxing

Unboxing25

Page 40: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :40Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Checked and Unchecked• We can use checked block to make sure that variables do not

exceed their limits. Any overflow is detected using check block. Unchecked, primarily used to allow overflow. Check is used when overflow check is suppressed with compiler options and it is explicitly enabled.

Page 41: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :41Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

• There are two types of type conversions in C#.• Implicit Conversions

– These conversions are performed by compiler for us, types with less bytes can be converted into higher types (or higher number of bytes).

• Explicit Conversions– This type of casting is performed just by using cast operators.

Type Conversions in C#

Page 42: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :42Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Coalesce (??) , is and as operators• ?? Returns first operand that is not null.• Primary use of Coalesce operator is to assign a nullable type to non

nullable type.• “is” is used to check if the object belongs to a specific type.• “as” keyword works like cast operator except one difference that it results

in a null reference if cast fails and does not generate an exception. Only compatible data types can be casted using as operator.

Page 43: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :43Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

Pass by Value, Pass by Reference ?

Page 44: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :44Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

• Ref keyword is used to pass variables to a method by reference, in general value type parameters are passed by copying data to the method argument variables, remember reference types are always passed by-ref, the same old law.

• In and out remind me of good old days of COM programming and IDL files. Out keyword can be pre-fixed with a method parameter. The keyword out is also used to pass value by reference with a slight difference. The variable being passed as out does not need to be initialized where as a variable being passed as a reference must be initialized in the calling code. The variable sent as out must be initialized by the called method, otherwise it will be treated as an error.

• Both out and ref are treated similarly at the compile time. See overloading constraints*

out and ref

Page 45: Lecture 3 __c_sharp

CSC 313 – Visual Programming3 .C# Programming

Slide :45Zohaib Hassan | Department of Computer Science Bahria University, Islamabad

out and ref


Recommended