KEMBAR78
C# | PDF | C Sharp (Programming Language) | Anonymous Function
0% found this document useful (0 votes)
232 views45 pages

C#

C# programming intro

Uploaded by

lordzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views45 pages

C#

C# programming intro

Uploaded by

lordzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

 About C#

 What is C#?
 More C#
 C# in the .NET framework
 Where C# is used?
 Version History
 The Syntax
 “Popular” IDEs

C# by Mazin Hakeem 2
 Some C# Features
 Object-Orientation
▪ Inheritance
▪ Polymorphism
 Properties
 Delegates
 Anonymous Methods
 Lambda Expressions
 Implicitly Typed Local Variables
C# by Mazin Hakeem 3
 Some C# Features (Continued)
 Object Initializers
 Anonymous Types
 LINQ
 Named Arguments & Optional Parameters
 Conclusion
 References

C# by Mazin Hakeem 4
C# by Mazin Hakeem 5
 Pronounced: C Sharp
 Called Visual C#, or just C#
 Developed by Microsoft for the .NET
framework initiative
 Is a pure object-oriented programming
language
 Also, a multi-paradigm programming
language (imperative, declarative, functional,
generic, & component oriented) (1)
C# by Mazin Hakeem 6
 Is safer than C++
 Is type-safe
 No misuse of pointers; must use the “unsafe” mode to
explicitly deal with pointers
 Has a Garbage Collector (GC); Memory management
is implicit
 In the .NET framework, C# is complied into a
binary-based intermediate language, Common
Intermediate Language (CIL), then the
framework converts it to machine code using
Common Language Runtime (CLR) (2 & 3)

C# by Mazin Hakeem 7
C# Source File

Compile .NET
C# Compiler
Time Framework

CIL

CLR
Run
Time
Machine Code

(Source: 2 & 3) OS Services

C# by Mazin Hakeem 8
 Desktop apps
 Websites (w/ ASP .NET)
 Web services
 Mobile phones (WM & WP7)
 DB apps (w/ ADO .NET)
 Distributed components
 UI design [Desktop/Web] (w/ Silverlight)
 … and many more

C# by Mazin Hakeem 9
 1.0 with .NET 1.0 w/ VSDN 2002 (2002)
 1.2 with .NET 1.1 w/ VSDN 2003 (2003)
 2.0 with .NET 2.0 w/ VSDN 2005 (2005)
 3.0 with .NET 3.5 w/ VSDN 2008 (2007)
 4.0 with .NET 4.0 w/ VSDN 2010 (2010)

 VSDN  Visual Studio .NET


 In each version after 1.2, a lot of new features
were added to the language
C# by Mazin Hakeem 10
 C# 2.0 (1 & 4)
 Generics
 Partial types
 Anonymous methods
 Iterators
 Nullable types

C# by Mazin Hakeem 11
 C# 3.0 (1 & 4)
 Implicitly typed local variables
 Object and collection initializers
 Auto-Implemented properties
 Anonymous types
 Extension methods
 Query expressions
 Lambda expressions
 Expression trees

C# by Mazin Hakeem 12
 C# 4.0 (1 & 5 & 6)
 Dynamic binding
 Named and optional arguments
 Generic co- and contravariance

 On the next coming slides, a number of


features introduced in these versions will be
covered

C# by Mazin Hakeem 13
 Very similar to C++ & Java
Class declaration
Main method

Comments

Variable declaration

C# by Mazin Hakeem 14
The result using VSDN 2010
Professional on Windows 7

The same result using Mono on


Ubuntu 10.10 (Linux)

C# by Mazin Hakeem 15
 C# is mainly used to develop under the .NET
framework environment for MS Windows®
 Mono allowed cross-platform development
 The “popular” IDEs:
 Visual Studio .NET
▪ For Windows XP to 7
▪ Free (limited) version (Express Edition) {since 2005}
▪ Various paid versions (Standard, Pro, Team, etc.)
 Mono
▪ Is open source and free
▪ Cross-platform (Win, Mac, and various Linux distros )
C# by Mazin Hakeem 16
C# by Mazin Hakeem 17
 Since C# is an object-oriented language, then
all object-oriented concepts are supported
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism

C# by Mazin Hakeem 18
 C# allows single class inheritance only
 Use colon “ : ”
Class Employee inherits
from class Person

C# by Mazin Hakeem 19
 To override an inherited method for the
polymorphic behavior, the “override”
keyword must be written within the method
declaration in the inherited class

C# by Mazin Hakeem 20
 Must declare the function to be overridden in
the base class first
 by using “virtual” keyword for a regular class

 or, by defining an abstract method in an abstract


class

C# by Mazin Hakeem 21
Base/Parent Class

Child Class
The overridden behavior

C# by Mazin Hakeem 22
Main method

Result

C# by Mazin Hakeem 23
 “A property is a member that provides a
flexible mechanism to read, write, or
compute the value of a private field” (7)
 Properties act as public data members, but
are methods called “accessors” (7)
 They represent getters and setters
 The private data is not exposed, but
protected
 Provides a layer of abstraction &
encapsulation (2 & 3)
C# by Mazin Hakeem 24
Property

Access to a property
C# by Mazin Hakeem 25
 Auto-Implemented Properties (8)
 Introduced in C# 3.0
 Used if there is not much code logic
 No need to define private data members

C# by Mazin Hakeem 26
 “A delegate can be thought of as an object
containing an ordered list of methods with the
same signature and return type” (2)
 Like C/C++ function pointers, but type-safe
 Declared outside the class structure w/
“delegate” keyword
 No method body
 Methods are passed as parameters;
encapsulated inside the delegate object (9 & 10)
 Mostly used for UI control event handlers (e.g.
Button, Text box, etc.) (similar to Listeners in
Java)
C# by Mazin Hakeem 27
Delegate declaration outside the class

Instantiating the delegate and


passing the method
Calling the delegate

The result

(Source: 2 & 11)


C# by Mazin Hakeem 28
 The concept introduced in C# 2.0
 Also called “Anonymous Delegates” (3 & 12)
 We Declare a method when instantiating a
delegate; “passing a code block as a delegate
parameter” (2 & 13)
 Reduces the creation of a separate method
 Mostly used for a “one time” use of a method
 A bit similar to the “Anonymous Classes”
concept in Java
C# by Mazin Hakeem 29
The structure
of an
Anonymous
Method

The result

(Source: 14)
C# by Mazin Hakeem 30
 Introduced in C# 3.0
 Another kind of “Anonymous Methods”
 Less verbose
 No need to mention the “delegate” keyword
like in the regular “Anonymous Methods”
 Use the lambda operator “=>”; Is read “goes
to” (2)

C# by Mazin Hakeem 31
 Anonymous Method vs. Lambda Expression
Anonymous
Method
Simplified to

Lambda Expression

A clean version of the


Lambda Expression

 All of them produce the same result


 The last one is more clean, short and readable
(Examples from: 2)

C# by Mazin Hakeem 32
Lambda Expression

The result

C# by Mazin Hakeem 33
 Introduced in C# 3.0
 Variable types are not declared explicitly
 The “var” keyword is used to define variables
 The compiler infers the type from the initialized
statement
 Similar to JavaScript’s “var” variable declaration
 Variable must be initialized & can’t be “null”
 Can’t have more than one type defined
Variable “i” is compiled as type “int”
l
(Source: 14 & 15)

C# by Mazin Hakeem 34
 Introduced in C# 3.0
 Used when there is no class constructor
 The idea is to assign values to any accessible
property or field at the object’s creation time

Initializing the variable at runtime

(Source: 2 & 16)

C# by Mazin Hakeem 35
 Introduced in C# 3.0
 The concepts is to create unnamed class types
 Combines the “Object Initializer” concept to
assign values to fields on creation time, & the
“Implicitly Typed Local Variable” concept to let
the compiler infer the variable type
 Anonymous Types are common in LINQ
Anonymous Type which is inferred
as a class by the compiler

(Source: 17)

C# by Mazin Hakeem 36
 “Language Integrated Query”
 Pronounced “Link”
 An extension for the .NET 3.5 framework
 Introduced in C# 3.0 in VSDN 2008
 Used to query data like DB queries (2)
 Similar to SQL (a.k.a. Query Expression) (18)

C# by Mazin Hakeem 37
 Data could be represented in any object types
(e.g. arrays, class objects), relational DBs, &
XML
 Also, to manipulate any data source (3)
 Can perform filtering, ordering, grouping, &
joining operations in a few lines of code (19)
 “Anonymous Types” & “Implicitly Typed
Local Variables” concepts are used for the
querying part of the code (Query Expression)
C# by Mazin Hakeem 38
The data source (An
array of integers)

The Query
Expression

The result

(Source: 20)
C# by Mazin Hakeem 39
 Introduced in C# 4.0
 Each is distinct, but useful together
 Used to reduce code & make it easy to code
 Named Arguments
 No need to remember parameters’ positions
 Name the argument with its value using colon “:”

A method w/ arguments

Passing arguments values by explicitly


mentioning their names not in the
(Source: 21)
C# by Mazin Hakeem original order of the actual method 40
 Optional Parameters
 Can omit some arguments when passing to a
method
 No need for method overloads (defining the same
method more than once but w/ different
parameters)
 Default values must be assigned last in the method
S
Declaring the optional argument in the
Omitted an argument (country) method by assigning a default value
(Source: 22)
C# by Mazin Hakeem 41
Declaring the optional argument in the
method by assigning a default value

Omitting the “country”


argument and passing
arguments values by explicitly
mentioning their names not in
original order

The result

C# by Mazin Hakeem 42
 C# is an Object-Oriented language
 Is now a cross-platform language
 Lots of features have evolved and added since the 1st
version in 2002
 The programmer can write readable, few lines of code
 Getters & setters are defined in a single “accessor”
method called “Property”
 Provides on-the-fly variable, method, & class creation
 No more method overloads or remembering
arguments positions in a method w/ Named &
Optional Arguments

C# by Mazin Hakeem 43
1. http://en.wikipedia.org/wiki/C_Sharp_(programming_language)
2. Illustrated C# 2008, Apress, ISBN: 978-1590599549
3. Beginning C# 2008 from Novice to Professional, Apress, ISBN:
978-1590598696
4. http://en.csharp-online.net/CSharp_Overview
5. http://msdn.microsoft.com/en-us/magazine/ff796223.aspx
6. http://msdn.microsoft.com/en-us/vcsharp/ff628440.aspx
7. http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
8. http://www.csharp-station.com/Tutorials/Lesson10.aspx
9. http://msdn.microsoft.com/en-us/library/ms173171(v=vs.80).aspx
10. http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
11. http://www.akadia.com/services/dotnet_delegates_and_events.
html

C# by Mazin Hakeem 44
12. http://www.switchonthecode.com/tutorials/csharp-tutorial-
anonymous-delegates-and-scoping
13. http://msdn.microsoft.com/en-us/library/0yw3tz5k(v=vs.80).aspx
14. http://msdn.microsoft.com/en-us/library/bb384061.aspx
15. http://www.dotnetfunda.com/articles/article631-new-features-
in-csharp-2008-.aspx
16. http://msdn.microsoft.com/en-us/library/bb384062.aspx
17. http://msdn.microsoft.com/en-us/library/bb397696.aspx
18. Beginning C# 3.0, Wrox, ISBN: 978-0470261293
19. C# in Depth, Manning, ISBN: 1933988363
20. http://msdn.microsoft.com/en-us/library/bb397676.aspx
21. http://msdn.microsoft.com/en-us/library/dd264739.aspx
22. http://msdn.microsoft.com/en-
us/vcsharp/ff628440.aspx#argsparams

C# by Mazin Hakeem 45

You might also like