The .NET Framework
The .NET Framework is an integral
Microsoft Windows component designed to support next-generation applications
and services.
The simplest types in the .NET
Framework, primarily numeric and Boolean
types, are value types. Value
types are variables that contain their data directly instead of containing a reference to the data
stored elsewhere in memory. There are three general value types:
■ Built-in types
■ User-defined types
■ Enumerations
Built-in
Value Types
1.System.SByte (SByte/sbyte) 1 –128 to 127 Signed byte
values
2.System.Byte 1 0 to 255 Unsigned bytes
3.System.Int16 (Short/short)
2 –32768 to 32767
4.System.Int32 (Integer/int) 4 –2147483648 to 2147483647 Whole numbers
5.System.UInt32 (UInteger/uint) 4 0 to 4294967295 Positive whole numbers
6.System.Int64 (Long/long) 8 –9223372036854775808 to 9223372036854775807 Large whole numbers
7.System.Single (Single/float) 4 –3.402823E+38 to 3.402823E+38
Floating point Numbers
8.System.Double (Double/double) 8 –1.79769313486232E+308 to 1.79769313486232E+308 Precise or large floating point numbers
9.System.Decimal (Decimal/decimal) 16 –79228162514264337593543950335 to79228162514264337593543950335 Financial and scientific calculations requiring great precision
other value types are
i. System.Char
ii.System.Boolean
iii.System.DateTime
There are nearly 300 more value types
in the Framework
Enumerations
Enumerations are related symbols that
have fixed values. Use enumerations to provide a list of choices for developers
using your class.
Eg 1:
enum Titles : int { Mr, Ms, Mrs, Dr };
Titles t = Titles.Dr;
Console.WriteLine("{0}.", t);
Eg2:
public enum Genders : int { Male, Female };
public Genders gender;
Collections and Generics
Collections—classes used for
grouping and managing related objects that allow you to iterate over those objects . The .NET Framework’s System.Collections
namespace
supports several types of collections.
Types
of Collections
Name
Description
ArrayList
A simple resizeable,
index-based collection of objects
SortedList A sorted collection of name/value pairs of objects
Queue A first-in, first-out
collection of objects
Stack
A last-in, first-out collection of objects
Hashtable A collection of name/value pairs of objects
that allows retrieval by name or index
StringCollection
A simple resizeable collection of strings
StringDictionary
A collection of name/values pairs of strings
that allows retrieval by name or index
ListDictionary An efficient collection to store
small lists of objects
HybridDictionary A collection that uses a ListDictionary for
storage when the number
of
items in the collection is small, and then migrates the items to a
Hashtable for
large collections
NameValueCollection A collection of name/values pairs of strings that allows
retrieval by name or index
ArrayList
ArrayList contains a simple list of values that we can add , insert , delete , view etc operations in C#
EXAMPLE
ArrayList coll = new ArrayList();
string s = "Hello";
coll.Add(s);
coll.Add("hi");
coll.Add(50);
coll.Add(new
object());
string[] anArray =new string[] { "more",
"or", "less" };
coll.AddRange(anArray);
coll.Add("Hello");
coll.Remove("Hello");
// Removes first item in ArrayList
coll.RemoveAt(0);
// Removes first four items in ArrayList
coll.RemoveRange(0,
4);
//Adding
Arraylist values into ListBox
ArrayList newColl = new ArrayList();
newColl.Add("Hello");
newColl.Add("Goodbye");
foreach (string item in newColl)
{
ListBox1.Items.Add (item);
}
//sorting items in arraylist
newColl
.Sort();
Queue Class
--------------------------------------------------------------------------------------------------
The Queue class
is a collection for dealing with first-in, first-out (FIFO) handling of sequential
objects
Queue
Properties
Name Description
Count
Gets the number of items in the queue
Dequeue
Retrieves an item from the front of
the queue, and removes it.
Enqueue
Adds an item to the end of the queue
Peek Retrieves
the first item from the queue without actually removing it.
Example:
Queue q = new Queue();
q.Enqueue("First");
q.Enqueue("Second");
q.Enqueue("Third");
q.Enqueue("Fourth");
while (q.Count > 0)
{
ListBox1.Items.Add(q.Dequeue());
}
The Stack Class
--------------------------------------------------------------------------------------------------
The Stack
class is a last-in, first-out (LIFO)
collection.The interface to the Stack class
is also very simple: it supports pushing items into the stack and popping them out.
Stack Properties
Name
Description
Count Gets the number of items in the stack
Stack Methods
Name
Description
Pop Retrieves an item from the top of the stack, removing it
at the same time.
Push Adds an item to the top of the stack
Peek
Retrieves the top item from the stack without removing it
Example:
Stack s = new Stack();
s.Push("First");
s.Push("Second");
s.Push("Third");
s.Push("Fourth");
while (s.Count > 0)
{
ListBox1.items.add(s.Pop());
}
Dictionary
------------------------------------------------------------------------------------------------------
The Dictionary classes
supported by the .NET Framework are used to map a key to a
value. Essentially, they exist to allow you to create
lookup tables that can map arbitrary
keys to arbitrary values. the Hashtable
class is used to do this mapping of key/value pairs.
Example
Hashtable emailLookup = new Hashtable();
emailLookup["sbishop@contoso.com"] =
"Bishop, Scott";
emailLookup["chess@contoso.com"] =
"Hess, Christian";
emailLookup["djump@contoso.com"] =
"Jump, Dan";
foreach (DictionaryEntry entry in
emailLookup)
{
ListBox1.Items.Add (entry.Value);
}
Or
/*
foreach (object name in emailLookup.Values)
{
ListBox1.Items.Add (name);
} */
SortedList Class
------------------------------------------------------------------------------------------------------
Although the SortedList class
is definitely a dictionary class, it shares some of its behavior with how simple lists work.
This means that you can access items stored in the SortedList in
order.
EXAMPLE
SortedList sort = new SortedList(new
DescendingComparer());
sort["First"] = "1st";
sort["Second"] = "2nd";
sort["Third"] = "3rd";
sort["Fourth"] = "4th";
foreach (DictionaryEntry entry in sort)
{
Console.WriteLine("{0} = {1}",
entry.Key, entry.Value);
}
HybridDictionary
------------------------------------------------------------------------------------------------
The HybridDictionary
is best used in situations where some lists are small and others are very
large.
EXAMPLE
HybridDictionary emailLookup = new
HybridDictionary ();
emailLookup["sbishop@contoso.com"] =
"Bishop, Scott";
emailLookup["chess@contoso.com"] =
"Hess, Christian";
emailLookup["djump@contoso.com"] =
"Jump, Dan";
foreach (DictionaryEntry entry in emailLookup)
{
ListBox1.Items.Add(entry.Value);
}
Equivalent Generic Types
0 comments:
Post a Comment