Professional Documents
Culture Documents
[ISRAR ALI]
Namespaces and Assemblies
• The namespace keyword is used to declare a scope.
• Making software components reusable can result in naming collisions
(two classes defined by two programmers can have the same name).
using System.Collections;
void MyMethod()
{
int num1 = 7, num2 = 9;
Swap(ref num1, ref num2);
// num1 = 9, num2 = 7
}
void Swap(ref int x, ref int y)
{
int temp = x; x = y; y = temp;
}
Keyword out
void MyMethod() uninitialised
{
int num1 = 7, num2;
Subtraction(num1, out num2);
// num1 = 7, num2 = 5
}
void Subtraction(int x, out int y)
{
y = x - 2;
// y must be assigned a value
}
Keyword params
void MyMethod()
{
int sum = Addition(1, 2, 3); // sum = 6
}
int Addition(params int[] integers)
{
int result = 0;
for (int i = 0; i < integers.Length; i++)
result += integers[i];
return result;
}
Method Overloading
class Program
{
static void Main(string[] args) { }
// Overloaded Add() method.
static int Add(int x, int y)
{ return x + y; }
static double Add(double x, double y)
{ return x + y; }
static long Add(long x, long y)
{ return x + y; }
}
OBJECT ORIENTED
PROGRAMMING
Object-oriented programming (OOP) is a programming
paradigm that uses objects and data structures consisting of data
fields and methods together with their interactions to design
applications and computer programs. Programming techniques
may include features such as Information Hiding, Data
Abstraction, Encapsulation, Modularity, Polymorphism, and
Inheritance
Objects
• Everything is an object.
Think of an object as a fancy variable . it stores data, but you can “make
requests” to that object, asking it to perform operations on itself.
• A program is a bunch of objects telling each other what to do by sending
messages.
• Each object has its own memory made up of other objects.
• Every object has a type.
• All objects of a particular type can receive the same messages.
• Nested types can see all the members of their closing type, both private and
public ones.
• External types only see nested types if they are declared as public.
• Fields of inner types and outer types belong to different objects. We must
specify the object to which they belong when we want to access them. In Java,
inner classes can access fields of outer class
Abstract class:
public
public class
class List
List<T>
{{
private
private object[]
T[] elements;
elements;
private
private int
int count;
count;
public
public void
void Add(object
Add(T element)
element)
{ {
ifif (count
(count ==
== elements.Length)
elements.Length) Resize(count
Resize(count ** 2);
2);
elements[count++]
elements[count++] == element;
element;
}}
public
public object
T this[int
this[int
index]
index]
{ {
get
get {{ return List<int> }intList
} = new= List();
List intList
return elements[index];
elements[index]; new List<int>();
set
set {{ elements[index]
elements[index] == value;
value; }}
}} intList.Add(1); // Argument
No boxing is boxed
intList.Add(2); // Argument
No boxing is boxed
public
public int
int Count
Count {{ intList.Add("Three"); // Compile-time error
Should be an error
get
get {{ return
return count;
count; }}
}} int i = intList[0];
(int)intList[0];// No//cast
Castrequired
required
}}
Generics
• Why generics?
• Type checking, no boxing, no downcasts
• Increased sharing (typed collections)
IList<T>
• Collection interfaces
IDictionary<K,V>
ICollection<T>
IEnumerable<T>
• Collection base classes
IEnumerator<T>
IComparable<T>
• Utility classes IComparer<T>
Collection<T>
• Reflection KeyedCollection<T>
ReadOnlyCollection<T>
Nullable<T>
EventHandler<T>
Comparer<T>
Generic Class
void MyMethod()
{
List<int> list = new List<int>();
AddMultiple<int>(list, 2, 4, 6);
}
Iterators
• Methods that incrementally compute and return a sequence of values
class Program
{
static IEnumerable<int> Range(int start, int count) {
for (int i = 0; i < count; i++) yield return start + i; 0
} 1
4
static IEnumerable<int> Squares(IEnumerable<int> source) { 9
foreach (int x in source) yield return x * x; 16
} 25
36
static void Main() { 49
foreach (int i in Squares(Range(0, 10))) Console.WriteLine(i); 64
} 81
}