- Reference and value types
- Structs are value types, while classes are reference types
- Stack
- Heap
- Heap -> pointers to ref types
Value Types –
· if a field, -> stack, else (part of an object), -> heap:
· box value type to reference type
In C#, all the "things" declared with the following list of type declarations are Value types (because they are from System.ValueType):
- bool , byte , char , decimal , double , enum , float , int , etc
Reference Types
· Heap
· Unbox box reference type to value type
- string
- class
- interface
- delegate
- object
Garbage Collection
1. stop all running threads (a FULL STOP),
2. find all objects in the Heap that are not being accessed by the main program and delete them.
3. reorganize all the objects left in the Heap to make space and adjust all the Pointers to these objects in both the Stack and the Heap.
Modifiers
· Access Modifiers
· public
· protected (accessible in derived classes)
· internal (accessible within assembly)
· private
|
abstract
|
Indicate that a class is intended only to be a base class of other classes.
|
|
const
|
Specify that the value of the field or the local variable cannot be modified.
|
|
event
|
Declare an event.
|
|
extern
|
Indicate that the method is implemented externally.
|
|
override
|
Provide a new implementation of a virtual member inherited from a base class.
|
|
readonly
|
Declare a field that can only be assigned values as part of the declaration or in a constructor in the same class.
|
|
sealed
|
Specify that a class cannot be inherited.
|
|
static
|
Declare a member that belongs to the type itself rather than to a specific object.
|
|
unsafe
|
Declare an unsafe context.
|
|
virtual
|
Declare a method or an accessor whose implementation can be changed by an overriding member in a derived class.
|
|
volatile
|
Indicate that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.
|
By now, everyone is familiar with grabbing a DataSet from a database and binding it at runtime to an ASP.NET list control. This works just fine. But DataSets are weakly typed, providing late bound access to their properties. What if you want to use your own strongly typed custom object with meaningful properties and methods instead of a generic DataSet? In this article, we'll do just that by developing a custom collection that implements the CollectionBase abstract base class. This base class is provided in the System.Collections namespace for just this purpose. Then we'll bind a collection of products to an ASP.NET Repeater control. Why a Repeater control? Because the "hello world" DataGrid examples are getting pretty stale.
Making classes Bindable
- Implement Abstract Class - CollectionBase
CollectionBase is an abstract base class designed to be inherited and extended to support strongly typed custom collections. It inherits three interfaces, ICollection, IEnumerable, and IList, which define interface methods that accept System.Object types as elements in the collection. The collection is implemented with an encapsulated ArrayList, which it exposes as a protected property called InnerList. This is where the collection elements are stored. So to create a custom collection, we simply inherit this abstract class and then extend it to meet our specific needs. Why not just implement against those interfaces directly rather than using CollectionBase? You could, of course, but then you'd have to override every interface method, even the ones you won't ever use. By deriving your custom collection classes from CollectionBase, you can implement just the methods that you need.
Serialize an Object to a String
public class clsPerson
{
public string FirstName;
public string MI;
public string LastName;
}
class class1
{
static void Main(string[] args)
{
clsPerson p=new clsPerson();
p.FirstName = "Jeff";
p.MI = "A";
p.LastName = "Price";
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(Console.Out, p);
Console.WriteLine();
Console.ReadLine();
}
}
To create a simple custom control:
· define a class that derives from System.Web.UI.Control and override its Render method.
· The Render method takes one argument of type System.Web.UI.HtmlTextWriter. The HTML that your control wants to send to the client is passed as a string argument to the Write method of HtmlTextWriter.
Composite controls
· equivalent to user controls that are authored using ASP.NET page syntax.
· The main difference between user controls and composite controls is that
o user controls are persisted as .ascx text files,
o composite controls are compiled and persisted in assemblies.
The key steps in developing a composite control are:
- Override the protected CreateChildControls method inherited from Control to create instances of child controls and add them to the Controls collection.
- If new instances of your composite control will repeatedly be created on a page, implement the System.Web.UI.INamingContainer interface. This is a tagging interface that has no methods. When it is implemented by a control, the ASP.NET page framework creates a new naming scope under that control. This ensures that the child controls will have unique IDs in the hierarchical tree of controls.
Generics
Generics allow you to define type-safe data structures, without committing to actual data types. This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code.
Without Generics a general purpose class to handle different types of data would operate on "NET Object",
which causes
(1) performance problems for value types, that need to be boxed in an object, and unboxed out of object, causing more garbage collections.
(2) type safety. Because the compiler lets you cast anything to and from Object, you lose compile-time type safety
(3) productivity impact. Writing type-specific data structures is a tedious, repetitive, and error-prone task.
Solution:
Generics allow you to define type-safe classes without compromising type safety, performance, or productivity. You
you have to instruct the compiler which type to use instead of the generic type parameter T, both when declaring the variable and when instantiating it:
public class Stack<T>
{...}
Stack<int> stack = new Stack<int>();
Nullable Types
Nullable types are instances of the System.Nullable struct.
A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.
class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
}
//y is set to zero
int y = num.GetValueOrDefault();
// num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}
Design Patterns
Creational - the way classes instantiate
o Singleton 18
§ Ensure a class has one instance
· Hide “New” Constuctor
· Add Shared solitary instance of self
o Abstract Factory 21
§ Create families of related or dependent objects without specifying classes
· Daddy, Mommy, Baby Bear (interfaces)
· Golden or Brown Bears (factories)
o Factory Method 26
§ Design an Interface for object creation, but let sub-classes decide which class to instantiate.
· Protected member can be inherited
· Overridable methods
· Structural – the way classes inherit
o Adapter 29
§ Convert an interface for a class into another interface clients expect
o Facade 29
§ Provide a unified interface A for sub-interfaces 1, 2, 3
o Bridge 34
§ Decouple an abstraction from an implementation so that the 2 can vary independently
· Purchase Orders Interface
· Suppliers Implementation
o Composite 39
§ Compose objects into a tree structure collection of containers and leaves to represent part-whole hierarchies to let clients treat objects and compositions of objects uniformly.
o Decorator 43
§ Attach additional responsibilities on Objects dynamically
· Interface -> Concrete -> Decorator -> Sub-Decorators
o Proxy 49
§ Provide ObjB surrogate of an ObjA to control (limit/override) access to ObjA.
· Behavioral – supervise messages between objects
o Observer 52
§ Define a one way dependency between objects so that when an objects change state, dependents are notified.
· Sender Interface notifies Observer Interface
o State 55
§ Allow object to alter behavior when internal distinguishable state changes.
· Sender Interface notifies Observer Interface
o Strategy 60
§ Encapsulate each of a family of algorithms so that algorithm varies for client.
· Sender Interface notifies Observer Interface