Академический Документы
Профессиональный Документы
Культура Документы
in
Module 4
Implementing properties in access field properties
struct ScreenPosition
{
public int x;
public int y;
public ScreenPosition(int x,int y)
{
this.x=checkRange(x);
this.y= checkRange(y);
}
public static int checkRange (int value)
{
if (value < 0 || value > 1024)
{
throw new ArgumentOutOfRangeException;
}
return value;
}
static void Main ()
{
ScreenPosition s = new ScreenPosition (25,40);
s.x = -100;
s.y=-200;//the screen position cant be in negative
}
}
If the fields are public, the user can misuse it. We are not enforcing encapsulation.
Ex: s.x = -100;
One solution is make the fields private and write the public methods to access it,this
solution enforces the encapsulation but it doesn’t has a natural field like syntax,instead it
uses awkward method based syntax.
We have to access the values of x and y by methods only. This is a major drawback.
Example:
struct Screenposition
{
private int x, y;
public int X
{
set
{
this.x = checkRange (value);
}
get
{
return this.x;
}
}
public int Y
{
set
{
this.y = checkRange (value);
}
}
private static int checkRange (int value)
{
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 4 | Module 4 Source : diginotes.in
if (value < 0 || value > 1024)
{
throw new ArgumentOutOfRangeException;
}
return value;
}
static void Main (String [ ] args)
{
int value;
ScreenPosition st = new ScreenPosition ();
s.X = 100;
value = s.X;
s.X+= 10;
}
}
Property:
It is a cross between a field and a method.
It looks like a field but acts like a method.
User will access the same like a field but the compiler will take as a method.
A property can contain two blocks of code which starts with set and get keywords.
Using Property
s.x = 100; //set
value = s.x; //get
s.x += 10; //both set and get
Property Accessibility
We can change the accessibility of only one of the accessor when we define it (get or
set).
It would not make much sense to define a property as public only to change the
accessibility of both accessor to private anyways.
The modifier must not specify an accessibility that is less respective than that of the
property. (Don’t make the property as private. Instead make either get or set as
private.)
struct ScreenPosition
{
private int x;
public int X
{
private set
{
this.x = checkRangex (value);
}
public get
{
……
……
}
}
void change ()
{
x = 100;
}
}
Using property appropriately:
Properties are powerful features but we have to use in the correct manner.
Ex:
class Account
{
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 6 | Module 4 Source : diginotes.in
private int bal;
public int Balance
{
set
{
this.bal = value;
}
get
{
return this.bal;
}
}
}
In the above example, the balance of the user is not updated correctly. Suppose a
person has Rs. 1000 and he uses get property to withdraw Rs. 500, the Rs. 1000
balance is not changed to Rs. 500.
So this is a poor programming method.
To overcome this we use separate withdraw and deposit methods.and give only read
permission for balance property.
public int Balance { get{return this.bal; } }
Property restrictions:
We can assign a value through a property of a structure or a class only after structure
of class variable is been initialized (object is created).
Without object, we get error.
Ex:
ScreenPosition s;
s.x = 100; //Error
ScreenPosition s = new ScreenPosition ();
s.x = 100; //Valid
We cannot use a property with ref or out arguments to a method.
Ex: void method (ref s.x) //Error
Property can contain atmost one get accessor and one set accessor. It cannot contain
other methods or fields or properties.
The get and set cannot take any parameters. The parameters being assigned is
passed to the set accessor automatically by using value variable.
We can’t declare property using const and static, but we can have if condition in get
and set accessor.
When we invoke object initializer, C# generates the code for calling the default
constructor and then it calls the set accessor of the property.
We can also call a parameterized constructor and then the object initializer.
Remember that always the constructor will be called first and then the property.
Write a C# program which defines a class Polygon which has 2 properties number of
sides and side length. Using object initializer, create 3 types of polygons Square,
Triangle, Pentagon and print each polygon’s sides and its values.
class Polygon
{
String name;
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 11 | Module 4 Source : diginotes.in
private int sides = 10, length = 10;
public Polygon (String name)
{
this.name = name;
}
public int Side
{
get
{
return this.sides;
}
set
{
this.sides = value;
}
}
public int Length
{
get
{
return this.length;
}
set
{
this.length = value;
}
}
}
public class Demo
{
static void Main (String [ ] args)
{
Polygon p1 = new Polygon (“Square”) { Side = 4, Length = 20 };
Polygon p2 = new Polygon (“Triangle”) { Side = 3, Length = 10 };
Polygon p3 = new Polygon (“Pentagon”) { Side = 5, Length = 40 };
Console.WriteLine (p1.name + “ ” + p1.Side + “ ” + p1.Length);
Console.WriteLine (p2.name, p2.Side, p2.Length);
Console.WriteLine (p3.name, p3.Side, p3.Length);
}
}
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 12 | Module 4 Source : diginotes.in
Output:
Square 4 20
Triangle 3 10
Pentagon 5 40
Indexer:
Indexer is same like property as smart field where a property encapsulates a single
value in a class but an indexer encapsulates a set of values.
An indexer is not a method. There is not parenthesized containing a parameter but
there are square brackets specifying the index.
All indexer uses this keyword.
A class or a structure can use at most 1 indexer or it can overload an indexer but
always the name of the indexer is this.
Indexer acts like a property which has get and set accessor.
The index specified in the indexer is populated when the indexer is called.
Ex:
struct IntBIts
{
private int bits;
public IntBits(int val)
{
bits = val;
}
public bool this[int index]
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 14 | Module 4 Source : diginotes.in
{
get
{
return (bits &(1<<index) != 0);
}
set
{
if(value)
bits = bits|(1<<index);
else
bits = bits&(1<<index);
}
}
public void display()
{
Console.WriteLine(“bits = {0}”, bits);
}
}
Class program
{
Static void Main(string [ ] args)
{
IntBits b = new IntBits(126);
bool ans = b [6] //retrieves bool at index 6;
b[0] = true;
b[3] = false;
b.display (); //ans will be 119;
}
}
Indexer Array
class IndexerDemo
{
private int [ ] data = new int [10];
public int this[int i]
{
set
{
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 17 | Module 4 Source : diginotes.in
this.data [i] = value;
}
get
{
return this.data [i];
}
}
static void Main (String [ ] args)
{
IndxerDemo ID = new IndexerDemo ();
int [ ] value =new int[5];
value [0] =ID[0];it just assignes the value not the reference
value[0]++//upadtes only in value array not in private array data[0]
}
}
Indexers in Interface
We can declare indexers in an interface specifying the get keyword and set keyword
or but replace the body of get and set accessor with a semicolon.
Any class or structure implements the interface must implement the indexer
accessors declared in the interface.
interface IData
{
int this [int Index]
{
get;
set;
}
}
class Example : IData
{
private int [ ] data = new int [10];
int IData. this [int index]
{
set
{
this.data [i] = value;
}
get
{
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 18 | Module 4 Source : diginotes.in
return this.data [i];
}
}
}
Generalized Queue
Replace int with object []
Ex:
class Queue
{
private object [ ] data;
private int head = 0, tail = 0;
private int count = 0;
private count int desize = 100;
public Queue ()
{
this.data = new object [desize];
}
public Queue (int s)
{
if (s > 0)
{
this.data = new object [s];
}
else
throw new ArgumentOutOfRangeException (“Invalid”);
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 21 | Module 4 Source : diginotes.in
}
public void Enqueue (object ele)
{
if (this.count == this.data.length)
{
throw new Exception (“Queue is full”);
}
this.data [this.head] = ele;
this.head = (this.head + 1 ) % this.data.length;
this.count ++;
}
public object dequeue ()
{
if (this.count == 0)
{
throw new Exception (“Queue empty”);
}
object ele = this.data [this.task];
this.tail = (this.tail + 1) % this.data.length;
this.count --;
return ele;
}
}
class mainDemo
{
Queue q = new Queue (5);
q.Enqueue (10);
q.Dequeue (10);
Queue q = new Queue ();
}
Generalized class
It is important to know that we have to cast the values returned by a method.
Ex:
Console.WriteLine (q.Dequeue ()); //Error
This compiler throws an error saying that implicit type casting is not possible.
Suppose if we perform invalid casting, say
circle mycircle = (circle) q.Dequeue ();
But the deleted element is Horse Object.
Generic class
Syntax
class classname <T >
{
interface IComparable
{
int CompareTo (object i);
}
Drawback:
Since the method can accept any type of object, sometimes we might get an
InvalidTypeCastException.
So we can use generic interface IComparable<T> to avoid this.
Ex: 1, 5, -2, 1, 6
Inorder Algorithm:
if left child is not empty
display the content of left subtree
display the root
if left child is not empty
display the content of left subtree
namespace BinaryTree
{
public class Tree<TItem> where TItem : IComparable<TItem>
{
public TItem NodeData { get; set; }
public Tree<TItem> LeftTree { get; set; }
public Tree<TItem> RightTree { get; set; }
public Tree(TItem nodeValue)
{
this.NodeData = nodeValue;
this.LeftTree = null;
this.RightTree = null;
}
public void Insert(TItem newItem)
{
TItem currentNodeValue = this.NodeData;
if (currentNodeValue.CompareTo(newItem) > 0)
{
// Insert the item into the left subtree
if (this.LeftTree == null)
{
this.LeftTree = new Tree<TItem>(newItem);
}
else
{
this.LeftTree.Insert(newItem);
}
}
else
{
// Insert the new item into the right subtree
if (this.RightTree == null)
{
this.RightTree = new Tree<TItem>(newItem);
}
else
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 27 | Module 4 Source : diginotes.in
{
this.RightTree.Insert(newItem);
}
public string WalkTree()
{
string result = "";
if (this.LeftTree != null)
{
result = this.LeftTree.WalkTree();
}
result += $" {this.NodeData.ToString()} ";
if (this.RightTree != null)
{
result += this.RightTree.WalkTree();
}
return result;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BinaryTree;
namespace BinaryTreeTest
{
class Program
{
static void Main(string[] args)
{
Tree<int> tree1 = new Tree<int>(10);
tree1.Insert(5);
tree1.Insert(11);
tree1.Insert(5);
tree1.Insert(-12);
tree1.Insert(15);
tree1.Insert(0);
tree1.Insert(14);
tree1.Insert( -8);
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 28 | Module 4 Source : diginotes.in
tree1.Insert(10);
tree1.Insert(8);
tree1.Insert(8);
string sortedData = tree1.WalkTree();
Console.WriteLine($"Sorted data is: {sortedData}");
Tree<string> tree2 = new Tree<string>("Hello");
tree2.Insert("hi");
tree2.Insert("hello");
tree2.Insert("how");
tree2.Insert("are");
tree2.Insert("you");
tree2.Insert("Im");
tree2.Insert("fine");
tree2.Insert("You");
tree2.Insert("Are");
tree2.Insert("Feeling");
tree2.Insert("now");
tree2.Insert("!");
sortedData = tree2.WalkTree();
Console.WriteLine($"Sorted data is: {sortedData}");
Console.ReadLine();
}
}
}
Generic method
With generic method we can specify the types of the parameters and return type by
using a type parameter in a manner similar to that used when we define a generic
class.
In this way we define generalized methods that are safe and avoid overhead of
casting
The syntax is same like the type parameter syntax as that of generic class
returntype methodname<T>(T parameters)
Example
static void swap<T>(ref T a,ref T b)
{
T temp=a;
a=b;
b=temp;
}
static void Main(string []agrs)
{
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 29 | Module 4 Source : diginotes.in
int a=5,b=6;
Swap<int>(ref a,ref b);
String s1=”hi”,s2=”hello”;
Swap<string>(ref s1,ref s2);
}
namespace BuildTree
{
class Program
{
static void Main(string[] args)
{
Tree<char> charTree = null;
Tree<int> it = null ;
InsertIntoTree<char>(ref charTree, 'M', 'X', 'A', 'M', 'Z', 'Z', 'N');
string sortedData = charTree.WalkTree();
Console.WriteLine($"Sorted data is {sortedData}");
InsertIntoTree<int>(ref it, 10,2,1,4,15,3,2,1,4);
string intdata = it.WalkTree();
Console.WriteLine($"Sorted data is {intdata}");
Console.ReadLine();
}
static void InsertIntoTree<TItem>(ref Tree<TItem> tree, params TItem[]
data) where TItem : IComparable<TItem>
{
foreach(TItem datum in data)
{
if(tree==null)
{
tree = new Tree<TItem>(datum);
}
else
{
tree.Insert(datum);
Program
interface IComparing <in T>
{
int Compare(T x,T y);
}
class Example : IComparing <object>
{
int IComparing<object>.Compare (object x,object y)
{
int xcode = x.GetHashCode();
int ycode = y.GetHashCode();
if (xcode == ycode)
return 0;
if (xcode < ycode)
return -1;
return 1;
}
static void Main(String [ ] args)
{
Example e = new Example ();
IComparing <object> ie = e;
IComparing <String> sc = e;
IComparing <A> ae = e;
}
}
List<T>
It is the simplest form of collection classes.
We can use square brackets and the index for displaying the elements of the list as
like array, but adding or deleting an element is not possible by using array syntax.
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 34 | Module 4 Source : diginotes.in
No need to specify the capacity when we create it because it will grow or shrink as
we add or delete elements.
Methods:
o Add (val) – Inserts a value at the end of the list.
o Insert (index, val) – Inserts a value at a specific index position.
o Remove (val) – Removes the values. If there are multiple same values, it
removes the first occurrence of the value. It returns the value that is removed.
o RemoveAt (index) – Removes value from the specified index position.
o Sort – Sorts the values in ascending order.
Uses property count
o It uses count to keep track of number of values in the list.
Ex:
static void Main()
{
List <int> l = new List <int> ();
l.add (2);
foreach (int x in new int [5] {10, 4, 15, -4, 3})
l.add (x);
l.Insert (3,100);
i l.Remove (100); //100 is removed
l.RemoveAt (3); //15 is removed (index 3)
//Displaying the list elements
Console.WriteLine (“Elements in list:”);
foreach (int x in l)
Console.WriteLine (x);
l.sort(); //Sorts the list values in ascending order
Console.WriteLine (“Elements in list after sorting:”);
foreach (int x in l)
Console.WriteLine (x);
}
Output:
Elements in list:
10
4
-4
3
Elements in list after sorting:
-4
3
4
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 35 | Module 4 Source : diginotes.in
10
LinkedList<T>
It is an implementation of Doubly Linked List.
It uses 5 properties:
o Previous – Holds the reference of previous node.
o Value – Hold the value.
o Next – Holds the reference of next node.
o First – Holds the reference of first record.
o Last – Holds the reference of last records
Methods:
o AddFirst (value) – Inserts the value at beginning movind the previous first item
up and setting its previous property to refer new item.
o AddLast (value) – Inserts the value at the end by setting new node previous
property will hold the reference of previous last node.
o AddBefore (LinkedListNode<T>node, value) – Inserts value before the node.
o AddAfter (LinkedListNode<T>node, value) – Inserts value after the node.
o (both requires to traverse to retrive the node)
o RemoveFirst () – Removes the first node.
o RemoveLast () – Removes the last node.
o Remove (val) – Removes the first occurance node whose value matches with
the argument passed.
Ex:
Static void Main ()
{
LinkedList <int> l = new LinkedList <int> ();
l.AddFirst (5);
foreach (int x in new int [3] {10, 20, 30})
l.AddLast (x);
l.AddBefore (l.Find (20), 15);
l.AddAfter (l.Find (20), 25);
l.RemoveFirst ();
l.RemoceLast ();
l.Remove (25);
Queue <T>
It implements first in first out mechanism an element is inserted at the back and removed
from the front.
Methods:
o Enqueue (val) – Inserts the value to the back of the queue.
o Dequeue-removes the element from the front
Property – Count
o Keeps a track of the number of elements in the queue.
Ex:
Main ()
{
Queue <int> q = new Queue <int> ();
q.Enqueue (10);
foreach (int x in new int [3] {20,30,40})
q.Enqueue (x);
//Display
foreach (int x in q)
{
Console.WriteLine (x);
}
Console.WriteLine(“first element deleted is {0}”,q.Deque());
}
Output:
10
Pushpanathan G, Dept. of CSE, CITech, Bangalore Source : diginotes.in
P a g e | 37 | Module 4 Source : diginotes.in
20
30
40
first element deleted is 10
Stack <T>
It implements last in first out mechanism an element joins the stack at the top(push(ele))
and leaves the stack at the top(pop()).
Methods:
o Push (ele)
o Pop ()
Property – Count
o Keeps track of number of elements in the stack.
Ex:
Main()
{
Stack <int> s = new Stack <int> ();
s.Push (10);
foreach (int x in new int [3] {20,30,40})
s.Push (x);
//Display
foreach (int x in s)
{
Console.WriteLine (x);
}
Console.WriteLine(“popped element deleted is {0}”,s.Pop());
}
Output:
10
20
30
40
popped element deleted is 40
Displaying:
foreach (KeyValuePair <String, int> x in d)
{
Console.WriteLine (“Name = {0} , Age = {1}”, x.key, x.value);
}
Properties: It uses 2 properties
o Key – To retrieve keys.
o Value – To retrieve values.
Program
Class Program
{
static void Main(string [ ] args)
{
Dictionary<string,int>ages=new Dictionary<string,int>();
ages.Add(“john”,51);
ages.Add(“ram”,41);
ages.Add(“sam”,34);
ages.Add(“john”,21);//Exception
ages[“john”]=21//overwrites 51 to 21
Console.WriteLine (“the contents are”);
foreach (KeyValuePair <String, int> x in ages)
{
Console.WriteLine (“Name = {0} , Age = {1}”, x.key, x.value);
}
}
}
Program
class Program
{
static void Main(string [ ] args)
{
SortedList<string,int>ages=new SortedList <string,int>();
ages.Add(“john”,51);
ages.Add(“ram”,51);
ages.Add(“john”,21);//Exception
ages[“john”]=21//overwrites 51 to 21
Console.WriteLine (“the contents are”);
foreach (KeyValuePair <String, int> x in ages)
{
Console.WriteLine (“Name = {0} , Age = {1}”, x.key, x.value);
}
}
}
HashSet<T>
Is optimized for performing set operations such as determing set membership and
generating the union and interaction of sets.
Methods:
o Add (val) – Inserts val to the hash set.
o UnionWith (Hashset) – Finds union of 2 hash sets and stores to first hash set.
Syntax: hs1.UnionWith (hs2)
Hs1 <- hs1 U hs2
o IntersectWith (HashSet) – Finds the intersection of 2 hash sets and stores to
first hash set.
Syntax: hs1.IntersectWith (hs2)
Hs1 <- hs1 ∩ hs2
Program:
static void Main()
{
HashSet <string> a= new List <string> (){“ram”,”sam”,”Kumar”};
HashSet <string> b= new List <string> (){“ramesh”,”sam”,”Kumar”,”ajay”};
foreach(string x in a)
{
Console.WriteLine(x);
}
foreach(string x in b)
{
Console.WriteLine(x);
}
a.UnionWith(b);
a.IntersectWith(b);
a.ExceptWith(a);
}
Collection Initializer
If collection has add methods we can initialize it similar to array
List<int> num=new List<int>( ){10,20,30,40,50,60};
Internally the c# compiler converts this intailiztion to a series of calls to the Add
method.
This is not supported for Queue<T> and Stack<T>