1. How does licensing work for virtualization ?
To make it easy, with the DataCenter edition, you can have unlimited licenses and includes the license for the host OS.
public interface ISerializable {
void GetObjectData(SerializationInfo, StreamingContext context);
}
protected Hashtable(
SerializationInfo info, StreamingContext context) {
}
void ISerializable.GetObjectData(
SerializationInfo info, StreamingContext context) {
// Serialize the desired values for this class
info.AddValue("title", title);
// Get the set of serializable members for our class and base classes
Type thisType = this.GetType();
MemberInfo[] mi =
FormatterServices.GetSerializableMembers(thisType, context);
// Serialize the base class's fields to the info object
for (Int32 i = 0 ; i < mi.Length; i++) {
// Don't serialize fields for this class
if (mi[i].DeclaringType == thisType) continue;
info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this));
}
}
class Program {
static void Main(string[] args) {
Fn f = Add; // Fn f = new Fn(Add);
Console.WriteLine(f(20, 30));
}
static int Add(int a, int b) {
return a + b;
}
delegate int Fn(int a, int b);
}
class Program {
static void Main(string[] args) {
Fn f1 = delegate(int a, int b) {
return a + b;
};
Console.WriteLine(f1(50, 60));
}
static int Add(int a, int b) {
return a + b;
}
delegate int Fn(int a, int b);
}
try{
.....
}
finally{
Monitor.Exit(obj)
}
class Program {
static void Main(string[] args) {
ThreadStart st = new ThreadStart(new A().ThreadMethod);
System.Threading.Thread th = new System.Threading.Thread(st);
th.IsBackground = true;
th.Start();
}
}
class A {
public void ThreadMethod() {
Thread.Sleep(-1);
}
}
HINSTANCE hinst;
HWND hwndMain;
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg;
BOOL bRet;
WNDCLASS wc;
UNREFERENCED_PARAMETER(lpszCmdLine);
// Register the window class for the main window.
if (!hPrevInstance)
{
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon((HINSTANCE) NULL,
IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE) NULL,
IDC_ARROW);
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = "MainMenu";
wc.lpszClassName = "MainWndClass";
if (!RegisterClass(&wc))
return FALSE;
}
hinst = hInstance; // save instance handle
// Create the main window.
hwndMain = CreateWindow("MainWndClass", "Sample",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, (HWND) NULL,
(HMENU) NULL, hinst, (LPVOID) NULL);
// If the main window cannot be created, terminate
// the application.
if (!hwndMain)
return FALSE;
// Show the window and paint its contents.
ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);
// Start the message loop.
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Return the exit code to the system.
return msg.wParam;
}
LRESULT CALLBACK MainWndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam) // second message parameter
{
switch (uMsg)
{
case WM_CREATE:
// Initialize the window.
return 0;
case WM_PAINT:
// Paint the window's client area.
return 0;
case WM_SIZE:
// Set the size and position of the window.
return 0;
case WM_DESTROY:
// Clean up window-specific data objects.
return 0;
//
// Process other messages.
//
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
class Program {
static void Main(string[] args) {
BankAccount b = new BankAccount();
b.LargeAmountEvent += new LargeAmountEventHandler(b_LargeAmountEvent1);
b.LargeAmountEvent += new LargeAmountEventHandler(b_LargeAmountEvent2);
b.Withdraw(100);
}
// Throws an exception
static private void b_LargeAmountEvent1(Object sender, BankEventArgs e) {
if (true) {
throw new Exception("exception");
}
Console.WriteLine("Withdrawn" + e.Amount);
}
// Handler which does not throw an exception
static private void b_LargeAmountEvent2(Object sender, BankEventArgs e) {
Console.WriteLine("Withdrawn" + e.Amount);
}
}/*End Program*/
public class BankEventArgs : EventArgs {
private int amount;
public BankEventArgs(int amount) {
this.amount = amount;
}
public int Amount {
get { return amount; }
}
}
public delegate void LargeAmountEventHandler(Object sender, BankEventArgs e);
public class BankAccount {
public event LargeAmountEventHandler LargeAmountEvent;
public void Withdraw(int amount){
OnLargeWithdraw(new BankEventArgs(amount));
}
protected void OnLargeWithdraw(BankEventArgs e){
if (LargeAmountEvent != null) {
// Instead of raising event, call GetInvocationList
Delegate[] d = LargeAmountEvent.GetInvocationList();
foreach (LargeAmountEventHandler evnt in d) {
try {
evnt.Invoke(this, e);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
//LargeAmountEvent(this, e);
}
}
}
class DataAccessCode{
public static string[] GetCustomerList(string state){
// call across network to DBMS or Web Services to retrieve data
// pass data back to caller using string array return value
}
}
public class Add: System.MulticastDelegate {
// Constructor
public Add(Object target, Int32 methodPtr);
// Method with same prototype as specified by the source code
public void virtual Invoke(Int32 a, Int32 b){
// If there are any delegates in the chain that
// should be called first, call them
if (_prev != null) _prev.Invoke(a, b);
// Call our callback method on the specified target object
_target.methodPtr(a, b);
}
// Methods allowing the callback to be called asynchronously
public virtual IAsyncResult BeginInvoke(Int32 a, Int32 b,
AsyncCallback callback, Object object);
public virtual void EndInvoke(IAsyncResult result);
}
class Test{
Int32 MyAdd(Int32 a, Int32 b);
static Int32 StMyAdd(Int32 a, Int32 b);
}
Test obj = new Test();
Add a = new Add(obj.MyAdd)
MethodImplAttribute(MethodImplOptions.Synchronized)]
public void add_MailMsg(EventHandler handler) {
MailMsg = (EventHandler)
Delegate.Combine(MailMsg, handler);
}
Type type1 = typeof(A);
Type type2 = typeof(B);
Type type3 = type1.GetType(); // System.RuntimeType
Type type4 = type2.GetType(); // System.RuntimeType
bool b = type3 == type4; // Returns true,showing for both A and B,there is 1 instance
Sealed class Singleton {
public static readonly Singleton = new Singleton();
}
Class A{
}
class B{
A obj;
}
According to this principle the way of designing a class structure is to start from high level modules to the low level modules:
High Level Classes --> Abstraction Layer --> Low Level
But why ?
1. The use of DIP makes the high level modules reusable as they are not directly dependent on low level details/modules. It helps in creation of reusable frameworks.
2. It helps in creation of code that is resilient to change. And, since the abstractions and details are all isolated from each other, the code is much easier to maintain.
Layering and DIPFor example, lets consider the three layers A->B->C. In this case the layer A is sensitive to all the changes down in layer C. Dependency is transitive.
Using DIP, A->IB->BImpl->IC->CImpl
Here, each of the lower layers are represented by an abstract class. Each of the higher level classes uses the next lowest layer through the abstract interface. Thus, none of the layers
depends upon any of the other layers. Instead, the layers depend upon abstract classes. Not only is the transitive dependency of A Layer upon C Layer broken, but even the direct dependency of A Layer upon B Layer broken.
The point is to exploit polymorphism by programming to a supertype so that the actual runtime object is not locked into the code. And the supertype can be an interface or an abstract class.
class AType {
static int x = 5;
}
class AType {
static int x;
static AType() {
x = 5;
}
}
7. The .NET Framework guarantees thread safety on static type initialization.
class Singleton{
public static readonly Singleton Instance = new Singleton();
}