Module 4
Module 4
Science
17IS564:C#.Net
What is Encapsulation?
• Encapsulation enables the language’s ability to hide
unnecessary implementation details from the object user.
• The concept of encapsulation revolves around the notion
that an object’s field data should not be directly accessible
from the public interface.
• In C#, encapsulation is enforced at the syntactic level
using the public, private, protected, and protected internal
keywords.
• // A class with a single public field. public class Book
{
public int numberOfPages;
}
static void Main(string[] args)
{
Book miniNovel = new Book();
miniNovel.numberOfPages = 30000000;
}
Enforcing Encapsulation Using
Traditional Accessors and Mutators
• If we want to access any private data, we
can write a traditional accessor (get
method)
• When we want to provide value to data,
we can write a mutator (set method). For
example,
// Traditional accessor and mutator for a point of private data. public class
Employee
{
private string fullName;
//Accessor.
public string GetFullName()
{
return fullName;
}
// Mutator.
public void SetFullName(string n)
{
fullName = n;
}
}
// static void Main(string[] args)
{
Employee p = new Employee();
p.SetFullName("Fred Flintstone");
onsole.WriteLine("Employee is named: {0}", p.GetFullName());
Console.ReadLine();
Another Form of Encapsulation:
Class Properties
• A property is a cross between a field and a
method—it looks like a field but acts like a
method.
• · You access a property by using exactly the
same syntax that you use to access a field.
• However, the compiler automatically translates
this field-like syntax into calls to accessor
methods (sometimes referred to as property
getters and property setters).
The syntax for a property
declaration looks like this:
<AccessSpecifier> <return_Type> <PropertyName>
{
get
{
// read accessor code
}
set
{
// write accessor code
}
}
•A property can contain two blocks of code, starting with the get and set keywords.
•The get block contains statements that execute when the property is read, and the set
block contains statements that run upon writing to the property
Auto Implemented Property
Static Property
Abstract Property
Interface Properties
}
Class program <T>
{
}
class MyGenericClass<T>
{
private T genericMemberVariable;
public MyGenericClass(T value)
{
genericMemberVariable = value;
}
public T genericMethod(T genericParameter)
{
Console.WriteLine("Parameter type: {0}, value: {1}", typeof(T).ToString(),genericParameter);
Console.WriteLine("Return type: {0}, value: {1}", typeof(T).ToString(), genericMemberVariable);
return genericMemberVariable;
}
public T genericProperty
{
get; set;
}
}
The following figure illustrates how the compiler will replace T with int in
MyGenericClass.
class CompareClass
{
public bool Compare(string x, string y) {
if (x.Equals(y)) return true;
else return false;
}
public bool Compare(int x, int y) {
if (x.Equals(y)) return true;
else return false;
}
}
Generic method
• Generic methods have type parameters. They
provide a way to parameterize the types used in
a method. One implementation is used with
many different types.
Note: The syntax form for the declaration uses the
<T> characters after the method name but
before the formal parameter list.
System.Collections
• Collection classes are specialized classes for data
storage and retrieval. These classes provide support
for stacks, queues, lists, and hash tables. Most
collection classes implement the same interfaces.
• Collection classes serve various purposes, such as
allocating memory dynamically to elements and
accessing a list of items on the basis of an index etc
• These classes create collections of objects of the
Object class, which is the base class for all data
types in C#.
ArrayList class
• It represents an ordered collection of an
object that can be indexed individually. It is
basically an alternative to an array.
• However unlike array you can add and
remove items from a list at a specified
position using an index and the array resizes
itself automatically.
• It also allow dynamic memory allocation,
adding, searching and sorting items in the list.
Proper Description
ty
Capacity Gets or sets the number of elements that the ArrayList can contain.
Count Gets the number of elements actually contained in the ArrayList.
IsFixedSi Gets a value indicating whether the ArrayList has a fixed size.
ze
Item Gets or sets the element at the specified index.
1 public virtual int Add( object value ); Adds an object to the end of
the ArrayList.
2 public virtual void AddRange( ICollection c );
Adds the elements of an ICollection to the end of the ArrayList.
3 public virtual void Clear();Removes all elements from the
ArrayList.
4 public virtual bool Contains( object item );
Determines whether an element is in the ArrayList.
5 public virtual void Insert( int index, object value );
Inserts an element into the ArrayList at the specified index.
6 public virtual void Remove( object obj );
Removes the first occurrence of a specific object from the
ArrayList.
7 public virtual void RemoveAt( int index );
Removes the element at the specified index of the ArrayList.
8 public virtual void Reverse();
Hashtable class
• The Hashtable class represents a collection of
key-and-value pairs that are organized based
on the hash code of the key. It uses the key to
access the elements in the collection.
• A hash table is used when you need to access
elements by using key, and you can identify a
useful key value. Each item in the hash table
has a key/value pair. The key is used to
access the items in the collection
Proper Description
ty
Count Gets the number of key-and-value pairs contained in the Hashtable.
IsFixedS Gets a value indicating whether the Hashtable has a fixed size.
ize
Item Gets or sets the value associated with the specified key.
Keys Gets an ICollection containing the keys in the Hashtable.
Values Gets an ICollection containing the values in the Hashtable.
Methods
1 public virtual void Add( object key, object value );
Adds an element with the specified key and value into the Hashtable.
Item Gets and sets the value associated with a specific key in the SortedList.
Keys Gets the keys in the SortedList.
Values Gets the values in the SortedList.
5 public virtual object GetKey( int index ); Gets the key at the specified index of
the SortedList.
6 public virtual int IndexOfKey( object key ); Returns the zero-based index of the
specified key in the SortedList.
7 public virtual int IndexOfValue( object value ); Returns the zero-based index of
the first occurrence of the specified value in the SortedList.
8 public virtual void Remove( object key ); Removes the element with the
specified key from the SortedList.
Stack class
• It represents a last-in, first out collection of
object. It is used when you need a last-in, first-
out access of items. When you add an item in
the list, it is called pushing the item and when
you remove it, it is called popping the item.
Property Description
Count Gets the number of elements contained in the Stack.