Академический Документы
Профессиональный Документы
Культура Документы
.NET Framework
Unity Player Architecture
Value and Reference Types
C# Collections
C# Generics
.NET PLATFORM
WHAT IS .NET FRAMEWORK?
SDK: A technology that supports building and running the next generation of
applications and XML Web services.
Runtime: A managed execution environment that provides a variety of services
to its running applications:
Memory management.
A common type system
An extensive class library.
Development frameworks and technologies (ASP.NET, ADO.NET, WCF)
Language interoperability (CIL)
Version compatibility.
Side-by-side execution.
Multitargeting (Windows, Windows Phone, Xbox)
.NET OBJECTIVES
Consists of
Common Language Runtime
.NET Framework Class
Library.
.NET
FRAMEWORK
HISTORY
.NET FOR UNITY
Unity supports .NET framework 3.5 (Subset)
with CLR 2.0
and C# 4 (Subset)
Unity supports
C# 4.0 (Subset)
.NET COMPILATION AND EXECUTION
Bytecode Native Code
Source Code
String
All arrays, even if their elements are value types
Class types, such as Form
Delegates
STRUCT VS CLASS
Struct Class
Value type Reference type
Usually used for smaller amounts Usually used for large amounts of
of data data
Does not support Inheritance Support Inheritance
Cannot have Destructor Can have Destructor
Passed to method by value Passed to method by reference
STRUCT VS CLASS
Struct Class
Cannot contain explicit Can create explicit parameterless
parameterless constructors in class
Can not instance Field initializer Can instance Field initializer
Can not be garbage collector so Can be garbage collector because
no memory management. garbage collector works on heap
memory
BOXING & UNBOXING
BOXING - UNBOXING PERFORMANCE
C# COLLECTIONS
SYSTEM.COLLECTIONS.GENERIC
Class Description
Dictionary<TKey, TValue> Collection of key/value pairs that are organized based on
the key.
List<T> List of objects that can be accessed by index. Provides
methods to search, sort, and modify lists.
Queue<T> FIFO collection of objects.
SortedList<TKey, TValue> Collection of key/value pairs that are sorted by key based
on the associated IComparer<T> implementation.
Stack<T> LIFO collection of objects.
SYSTEM.COLLECTIONS
Class Description
ArrayList Array of objects whose size is dynamically increased as required.
Hashtable Collection of key/value pairs that are organized based on the hash
code of the key
Queue FIFO collection of objects.
Stack LIFO collection of objects.
SYSTEM.COLLECTIONS.SPECIALIZED
Class Description
HybridDictionary Implements IDictionary by using a ListDictionary is small, and
then switching to a Hashtable when gets large.
ListDictionary Implements IDictionary using a singly linked list (< 10 items.)
NameValueCollection Represents a collection of associated String keys and String
values that can be accessed either with the key or index.
OrderedDictionary Collection of key/value pairs that are accessible by key or index.
StringCollection Represents a collection of strings.
StringDictionary Implements a hash table with the key and the value strongly
typed to be strings rather than objects.
BitVector32 Provides a simple structure that stores Boolean values and small
integers in 32 bits of memory.
LIST<T>
Construct a List, copying the contents of the given collection. The size and capacity will
both be equal to the size of the given collection.
public List(IEnumerable<T> collection)
LIST<T>
Adds the given object to the end of this list. The size of the list is increased by
one. If required, the capacity of the list is doubled before adding the new
element.
public void Add(T item) {
if (_size == _items.Length) EnsureCapacity(_size + 1);
_items[_size++] = item;
...
}
LIST<T>
Upon adding the first element to the list the capacity is increased to 4, and then
increased in multiples of two as required.
If the current capacity of the list is less than min, the capacity is increased to
twice the current capacity or to min, whichever is larger.
private const int _defaultCapacity = 4;
private void EnsureCapacity(int min) {
if (_items.Length < min) {
int newCapacity = _items.Length == 0?
_defaultCapacity : _items.Length * 2;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
...
DICTIONARY<TKEY, TVALUE>
Entry size will be expanded to the prime number that greater than twice of
old size.
private void Resize() {
Resize(HashHelpers.ExpandPrime(count), false);
}
Implemented by array
private T[] _array;
Default capacity is 4
Go double each time expanding capacity
remove insert at remove Random In-order Search for
add to end Notes
from end middle from middle Access Access specific element
Do you need a sequential list where the element is typically discarded after its
value is retrieved?
Yes:
Consider using the Queue class or the Queue<T> generic class if you need first-in, first-
out (FIFO) behavior.
Consider using the Stack class or the Stack<T> generic class if you need last-in, first-out
(LIFO) behavior.
No: consider using the other collections.
SELECTING A COLLECTION CLASS
Do you need to access the elements in a certain order, such as FIFO, LIFO, or
random?
The Queue class and the Queue<T> generic class offer FIFO access.
The Stack class and the Stack<T> generic class offer LIFO access.
The LinkedList<T> generic class allows sequential access either from the head to the tail,
or from the tail to the head.
SELECTING A COLLECTION CLASS
Will each element contain one value, a combination of one key and one value,
or a combination of one key and multiple values?
One value: Use any of the collections based on the IList interface or the IList<T> generic
interface.
One key and one value: Use any of the collections based on the IDictionary interface or
the IDictionary<TKey, TValue> generic interface.
One value with embedded key: Use the KeyedCollection<TKey, TItem> generic class.
One key and multiple values: Use the NameValueCollection class.
SELECTING A COLLECTION CLASS
Do you need to sort the elements differently from how they were entered?
The Hashtable class sorts its elements by their hash codes.
The SortedList class and the SortedDictionary<TKey, TValue> and SortedList<TKey,
TValue> generic classes sort their elements by the key, based on implementations of the
IComparer interface and the IComparer<T> generic interface.
ArrayList provides a Sort method that takes an IComparer implementation as a
parameter. Its generic counterpart, the List<T> generic class, provides a Sort method that
takes an implementation of the IComparer<T> generic interface as a parameter.
SELECTING A COLLECTION CLASS
class Customer { }
class Order { }
// Stack of value types
Stack<Customer> customers;
Stack<Order> orders = new Stack<Order>(); customers
class Stack<T>
customers = new Stack<Customer>(); others
GENERIC CONSTRAINTS
Covariance: Enables you to use a more derived type than originally specified
IEnumerable<Derived> d = new List<Derived>();
IEnumerable<Base> b = d;
Contravariance: Enables you to use a more generic (less derived) type than originally
specified.
Action<Base> b = (target) => { };
Action<Derived> d = b;
Invariance: Means that you can use only the type originally specified
DEFINE GENERICS VARIANCES