Date post: | 21-Mar-2017 |
Category: |
Software |
Upload: | mahpara-saaleem |
View: | 86 times |
Download: | 1 times |
C# LanguageCSC 313Visual ProgrammingFall 2016
Zohaib Hassan
Visiting Lecturer
Department of Computer Science
Bahria University, Islamabad
3LECTURE
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
CSC 313 – Visual Programming3 .C# Programming
Slide :3
Zohaib Hassan | Department of Computer Science Bahria University, IslamabadPage
History of C#
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
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..
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..
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..
CSC 313 – Visual Programming3 .C# Programming
Slide :8Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
C# 5.0• Asynchronous methods• Caller info attributes
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!
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..
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
CSC 313 – Visual Programming3 .C# Programming
Slide :12Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
C# Books
CSC 313 – Visual Programming3 .C# Programming
Slide :13Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Customers of C#
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.
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.
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!"); } }}
CSC 313 – Visual Programming3 .C# Programming
Slide :17
Zohaib Hassan | Department of Computer Science Bahria University, IslamabadPage
Using Statements for Static Members
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)); }}
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)); }}
CSC 313 – Visual Programming3 .C# Programming
Slide :20Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
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
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
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
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
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)
CSC 313 – Visual Programming3 .C# Programming
Slide :26Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Value Data types List
CSC 313 – Visual Programming3 .C# Programming
Slide :27Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
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
CSC 313 – Visual Programming3 .C# Programming
Slide :29Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Operators in C#
CSC 313 – Visual Programming3 .C# Programming
Slide :30Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Assignment in C#
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*
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.
CSC 313 – Visual Programming3 .C# Programming
Slide :33Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Structures in C#
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
CSC 313 – Visual Programming3 .C# Programming
Slide :35Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Literals in C#
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
CSC 313 – Visual Programming3 .C# Programming
Slide :37Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Variable Declarations in C#
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
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
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.
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#
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.
CSC 313 – Visual Programming3 .C# Programming
Slide :43Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
Pass by Value, Pass by Reference ?
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
CSC 313 – Visual Programming3 .C# Programming
Slide :45Zohaib Hassan | Department of Computer Science Bahria University, Islamabad
out and ref