CS Fundamentals v2
CS Fundamentals v2
CS Fundamentals v2
AND
C#
HELLO
C# defines one of the most powerful, feature-
Family Tree
C
C structured programming
C++ - first OO Model 1979
Java - portability 1992
- new and improved OO Model
Code translated into bytecode
and executed by JVM
Jav
Platform/CPU/OS independence a
C# - 2000
C+
+
C#
Why C#?
C# was created by Microsoft to fix some issues
Family Resemblance
From C, C# derives its syntax, many of its
What is .NET ?
The .NET Framework defines an
..NET Architecture
and at the
center:
Now, .NET has reached version 4.5 and C#, as a language has
Compiletime and
Runtime
When you compile a C# program, the output
THE C# LANGUAGE
Generally C++ syntax: types, variables,
statements, operators
OOP programming with new TYPES ( that
include both data and methods that work with
that data) and follow the OO principles.
Encapsulation
Inheritance
Polymorphism
ENCAPSULATION
Binding together code & the data it
Classes
Fields
Methods
Constructors
ACCESS MODIFIERS
METHODS
[Access modifier] [return type] [name](parameter list)
{
}
OVERLOADING the ability to use multiple methods
with the same name but different parameter list.
void Add(int a)
void Add(int a , int b)
{ a = a+5;
{ a= a+b;
}
}
void Add(double a){}
Add(5); Add(5,6); .
Constructors
Block of code that is executed when a new
object is created.
Used to set default values.
Can have multiple constructors (overloading).
If there is no constructor defined, C# will
automatically add a default constructor.
Can not be inherited!
Syntax
...
Reference Types
client1
Client client1 ;
Client client2 ;
client2 = client 1;
Client
Name=To
m
Client
Name=Bo
b
PROPERTIES
Syntax:
Understanding static
There will be times when you will want to define a
INHERITANCE
The ability of an object to acquire all of the
string
music_type;
Class Violinist
void Play_Violin()
INHERITANCE BASICS IN
C#
Class DerivedClass : BaseClass
{}
BaseClass gain all of DerivedClass members (methods, properties,
fields. )
class TwoDShape
{
public double Width;
public double Height;
public void ShowDim() {
Console.WriteLine(Width + " and " + Height);
}
}
2 other keywords
sealed - prevents inheritance. A sealed class
Constructor Inheritance
Constructors are not inherited by default.
Public DerivedClass(param list) : base (param
list)
{}
base keyword -> call a constructor from the
base class.
public Triangle(double x) : base(x) {
Style = "isosceles";
}
class TwoDShape
{
public virtual double Area() { return Width*Height;
}
}
class Triangle : TwoDShape
{
public override double Area() {
return Width * Height / 2;
}
}
class Circle:Two: TwoDShape
{
public override double Area() {
return (Math.Pi * Width *Width )/4;
}
}
POLYMORPHISM
Poly - WHAT?
Polymorphism (from Greek, meaning many forms)
Polymorphismin the context ofobject-oriented
class Base {
Generics
A single class/method/field for ANY TYPE.
WPF
WPF the next
generation User
Interface Technology
for WINDOWS
Thank you!
C# Fundamentals
Part II
C# Language Features:
Properties
Interfaces
Delegates & Events
Generics
Anonymous methods & Lambda Expressions
LINQ Integration
Delegates
A delegate is an object that can refer to a
Delegate Syntax
A delegate type is declared using the keyword
A Quick Example
// Declare a delegate type.
delegate string StrMod(string str);
static string ReplaceSpaces(string s) {
Return s.Replace(' ', '-'); }
static string RemoveSpaces(string s) { . }
static string Reverse(string s) {.}
StrMod str1 = new StrMod(ReplaceSpaces);
string test = str1("This is a test.");
Console.WriteLine("Resulting string: " + test);
str1 = new StrMod(RemoveSpaces);
test= str1("This is a test."); .
OUTPUT
Resulting string: This-is-a-test.
Resulting string: Thisisatest.
Example Explained
Notice that StrMod takes one string parameter and
Multicasting
Multicasting
StrMod strOp;
StrMod replaceSp = new
StrMod(ReplaceSpaces);
StrMod removeSp = new
StrMod(RemoveSpaces);
StrMod reverseStr = new StrMod(Reverse);
.
strOp = replaceSp;
strOp += reverseStr;
.
strOp(ref str); Output:tset-a-si-sihT
Covariance
There are two features that add flexibility to
Events
Another important C# feature is built upon
Syntax
Events are members of a class and are
Basic Example
// Declare a delegate type for an event.
delegate void MyEventHandler();
class MyClass{
public event MyEventHandler SomeEvent;
// This is called to raise the event.
public void RaiseEvent() {
if(SomeEvent != null)
SomeEvent(); } }
class InterestedClass{ static void Handler() {Console.WriteLine("Event occurred in
InterestedClass1!!!!!!!");}
class InterestedClass2{ static void Handler() {Console.WriteLine("Event occurred in
InterestedClass2!!!!!!!");}
static void Main() {
MyClass evt = new MyClass();
// Add Handler() to the event list.
evt.SomeEvent += new MyEventHandler(InterestedClass1.Handler);
evt.SomeEvent += new MyEventHandler(InterestedClass2.Handler);
// Raise the event.
evt.OnSomeEvent(); }}
OUTPUT: Event occurred in InterestedClass1!!!!!!!
Event occurred in InterestedClass2!!!!!!!
Anonymous Methods
You will often find that the method referred to by a delegate is
used only for that purpose. In other words, the only reason for
the method is so it can be invoked via a delegate. The method
is never called on its own. In such a case, you can avoid the
need to create a separate method by using an anonymous
function.
An anonymous function is, essentially, an unnamed block of
code that is passed to a delegate constructor. One advantage
to using an anonymous function is simplicity. There is no need
to declare a separate method whose only purpose is to be
passed to a delegate.
Beginning with version 3.0, C# defines two types of
anonymous functions: anonymous methods and lambda
expressions.
Anonymous Methods
An anonymous method is one way to create an
Passing Arguments
It is possible to pass one or more arguments to an
Lambda Expressions
Although anonymous methods are still part of
Statement Lambdas
Multiline lambdas
// IntOp takes one int argument and returns an int
result.
delegate int IntOp(int end);
IntOp fact = n => {
int r = 1;
for(int i=1; i <= n; i++)
r = i * r;
return r;
};
Generics
Now we will examine one of C#s most sophisticated and
Example Explained
Here, T is the name of a type parameter. This name is
General Syntax
The generics syntax shown in the preceding
Other Features
Multiple generic parameters
Generic methods
Generic Constraints
When specifying a type parameter, you can
Constraint Types
C# defines the following types of constraints.
You can require that a certain base class be
Quick Example
class A { .
public void Hello() {
Console.WriteLine("Hello");
}}
// Class B inherits A.
class B : A { }
// Class C does not inherit A.
class C { }
obj.Hello(); }}
Collection Classes
The .NET Framework provides specialized
theSystem.Collectionsor
System.Collections.Genericnamespace.
togetherhow?
Array? a[50]= object1;
Right?....
Not really
etc
List<string> , List<object>
Example:
List<int> list = new List<int>();
Example:
Dictionary<string, int> dictionary = new
Dictionary<string, int>();
dictionary.Add("cat", 2); dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
dictionary[cat] has the value 2 (where cat is the
key)
When Dictionary, is used in a foreach loop, it
LINQ via C#
Why LINQ?
The Motivation
Extensibility
Enter LINQ
LINQ is one of the most exciting features in C#.
LINQ stands for Language-Integrated Query. It encompasses a
LINQ Fundamentals
At LINQs core is the query. A query specifies
Basic Example
int[] nums = { 1, -2, 3, 0, -4, 5 };
Generalized Syntax
Generalizing, here is the syntax of the from
clause:
from range-variable in data-source
The next clause in the query is where. It
specifies a condition that an element in the
data source must meet in order to be obtained
by the query. Its general form is shown here:
where boolean-expression
Next, the selection:
select selection
Other Examples
Account[] accounts = { new Account("Tom",
Query Methods
The query syntax described by the preceding
to form a chain.
It is possible to write Dynamic Querries
and Expression Trees
MORE C#
The dynamic keyword
Multithreading
Networking