CLICK HERE TO DOWNLOAD PPT ON Introduction to C Sharp
Introduction to C Sharp Presentation Transcript
1.Introduction to C#
2.Prerequisites
This module assumes that you understand the fundamentals of
Programming
Variables, statements, functions, loops, etc.
Object-oriented programming
Classes, inheritance, polymorphism, members, etc.
C++ or Java
This module assumes that you understand the fundamentals of
Programming
Variables, statements, functions, loops, etc.
Object-oriented programming
Classes, inheritance, polymorphism, members, etc.
C++ or Java
3.Learning Objectives
C# design goals
Fundamentals of the C# language
Types, program structure, statements, operators
Be able to begin writing and debugging C# programs
Using the .NET Framework SDK
Using Visual Studio.NET
Be able to write individual C# methods
C# design goals
Fundamentals of the C# language
Types, program structure, statements, operators
Be able to begin writing and debugging C# programs
Using the .NET Framework SDK
Using Visual Studio.NET
Be able to write individual C# methods
4.Agenda
Hello World
Design Goals of C#
Types
Program Structure
Statements
Operators
Using Visual Studio.NET
Using the .NET Framework SDK
Hello World
Design Goals of C#
Types
Program Structure
Statements
Operators
Using Visual Studio.NET
Using the .NET Framework SDK
5.Hello World
6.Agenda
Hello World
Design Goals of C#
Types
Program Structure
Statements
Operators
Using Visual Studio.NET
Using the .NET Framework SDK
Hello World
Design Goals of C#
Types
Program Structure
Statements
Operators
Using Visual Studio.NET
Using the .NET Framework SDK
7.Design Goals of C#
Component-orientation
Everything is an object
Robust and durable software
Preserving your investment
Component-orientation
Everything is an object
Robust and durable software
Preserving your investment
8.C# is the first “Component-Oriented” language in the C/C++ family
What is a component?
An independent module of reuse and deployment
Coarser-grained than objects (objects are language-level constructs)
Includes multiple classes
Often language-independent
In general, component writer and user don’t know each other, don’t work for the same company, and don’t use the same language
What is a component?
An independent module of reuse and deployment
Coarser-grained than objects (objects are language-level constructs)
Includes multiple classes
Often language-independent
In general, component writer and user don’t know each other, don’t work for the same company, and don’t use the same language
9.Component concepts are first class
Properties, methods, events
Design-time and run-time attributes
Integrated documentation using XML
Enables “one-stop programming”
No header files, IDL, etc.
Can be embedded in ASP pages
10.Traditional views
C++, Java™: Primitive types are “magic” and do not interoperate with objects
Smalltalk, Lisp: Primitive types are objects, but at some performance cost
C# unifies with no performance cost
Deep simplicity throughout system
Improved extensibility and reusability
New primitive types: Decimal, SQL…
Collections, etc., work for all types
Properties, methods, events
Design-time and run-time attributes
Integrated documentation using XML
Enables “one-stop programming”
No header files, IDL, etc.
Can be embedded in ASP pages
10.Traditional views
C++, Java™: Primitive types are “magic” and do not interoperate with objects
Smalltalk, Lisp: Primitive types are objects, but at some performance cost
C# unifies with no performance cost
Deep simplicity throughout system
Improved extensibility and reusability
New primitive types: Decimal, SQL…
Collections, etc., work for all types
11.Garbage collection
No memory leaks and stray pointers
Exceptions
Type-safety
No uninitialized variables, no unsafe casts
Versioning
Avoid common errors
E.g. if (x = y) ...
One-stop programming
Fewer moving parts
No memory leaks and stray pointers
Exceptions
Type-safety
No uninitialized variables, no unsafe casts
Versioning
Avoid common errors
E.g. if (x = y) ...
One-stop programming
Fewer moving parts
12.C++ Heritage
Namespaces, pointers (in unsafe code), unsigned types, etc.
Some changes, but no unnecessary sacrifices
Interoperability
What software is increasingly about
C# talks to XML, SOAP, COM, DLLs, and any .NET Framework language
Increased productivity
Short learning curve
Millions of lines of C# code in .NET
Namespaces, pointers (in unsafe code), unsigned types, etc.
Some changes, but no unnecessary sacrifices
Interoperability
What software is increasingly about
C# talks to XML, SOAP, COM, DLLs, and any .NET Framework language
Increased productivity
Short learning curve
Millions of lines of C# code in .NET
13.Hello World
Design Goals of C#
Types
Program Structure
Statements
Operators
Using Visual Studio.NET
Using the .NET Framework SDK
Design Goals of C#
Types
Program Structure
Statements
Operators
Using Visual Studio.NET
Using the .NET Framework SDK
14.A C# program is a collection of types
Classes, structs, enums, interfaces, delegates
C# provides a set of predefined types
E.g. int, byte, char, string, object, …
You can create your own types
All data and code is defined within a type
No global variables, no global functions
Classes, structs, enums, interfaces, delegates
C# provides a set of predefined types
E.g. int, byte, char, string, object, …
You can create your own types
All data and code is defined within a type
No global variables, no global functions
15.Types contain:
Data members
Fields, constants, arrays
Events
Function members
Methods, operators, constructors, destructors
Properties, indexers
Other types
Classes, structs, enums, interfaces, delegates
Data members
Fields, constants, arrays
Events
Function members
Methods, operators, constructors, destructors
Properties, indexers
Other types
Classes, structs, enums, interfaces, delegates
16.Types can be instantiated…
…and then used: call methods, get and set properties, etc.
Can convert from one type to another
Implicitly and explicitly
Types are organized
Namespaces, files, assemblies
There are two categories of types: value and reference
Types are arranged in a hierarchy
…and then used: call methods, get and set properties, etc.
Can convert from one type to another
Implicitly and explicitly
Types are organized
Namespaces, files, assemblies
There are two categories of types: value and reference
Types are arranged in a hierarchy
17.Value types
Directly contain data
Cannot be null
Reference types
Contain references to objects
May be null
Directly contain data
Cannot be null
Reference types
Contain references to objects
May be null
18.Unified Type System
19.Benefits of value types
No heap allocation, less GC pressure
More efficient use of memory
Less reference indirection
Unified type system
No primitive/object dichotomy
No heap allocation, less GC pressure
More efficient use of memory
Less reference indirection
Unified type system
No primitive/object dichotomy
20.Implicit conversions
Occur automatically
Guaranteed to succeed
No information (precision) loss
Explicit conversions
Require a cast
May not succeed
Information (precision) might be lost
Both implicit and explicit conversions can be user-defined
Occur automatically
Guaranteed to succeed
No information (precision) loss
Explicit conversions
Require a cast
May not succeed
Information (precision) might be lost
Both implicit and explicit conversions can be user-defined
21.Everything is an object
All types ultimately inherit from object
Any piece of data can be stored, transported, and manipulated with no extra work
All types ultimately inherit from object
Any piece of data can be stored, transported, and manipulated with no extra work
22.Polymorphism
The ability to perform an operation on an object without knowing the precise type of the object
The ability to perform an operation on an object without knowing the precise type of the object
23.Question: How can we treat value and reference types polymorphically?
How does an int (value type) get converted into an object (reference type)?
Answer: Boxing!
Only value types get boxed
Reference types do not get boxed
How does an int (value type) get converted into an object (reference type)?
Answer: Boxing!
Only value types get boxed
Reference types do not get boxed
24.Boxing
Copies a value type into a reference type (object)
Each value type has corresponding “hidden” reference type
Note that a reference-type copy is made of the value type
Value types are never aliased
Value type is converted implicitly to object, a reference type
Essentially an “up cast”
Copies a value type into a reference type (object)
Each value type has corresponding “hidden” reference type
Note that a reference-type copy is made of the value type
Value types are never aliased
Value type is converted implicitly to object, a reference type
Essentially an “up cast”
25.Unboxing
Inverse operation of boxing
Copies the value out of the box
Copies from reference type to value type
Requires an explicit conversion
May not succeed (like all explicit conversions)
Essentially a “down cast”
Inverse operation of boxing
Copies the value out of the box
Copies from reference type to value type
Requires an explicit conversion
May not succeed (like all explicit conversions)
Essentially a “down cast”
26.Boxing and unboxing
27.Benefits of boxing
Enables polymorphism across all types
Collection classes work with all types
Eliminates need for wrapper classes
Replaces
Enables polymorphism across all types
Collection classes work with all types
Eliminates need for wrapper classes
Replaces
0 comments