Managed code is produced by a compiler that meets the Common Type System (CTS) requirements that are necessary before code produced by it can be run by the Common Language Runtime (CLR). Unmanaged code does not meet the CTS standard. The Windows API and COM objects are examples of unmanaged code.
The Common Type System defines the types and their members that must be used by compilers that create code to run on the Common Language Runtime. The Common Language Specification provides stricter requirements that ensure interoperability between languages.
All .NET compilers generate an Intermediate Language (IL) code. Since IL is compatible, irrespective of its source, the CLR doesn't care which compiler produces it.
The Global Assembly Cache (GAC) holds shared assemblies—assemblies that can be used by more than one application. Assemblies in the GAC have a digital signature that uniquely identifies them, even if they have the same file name.
A namespace usually identifies a group of types that provide related services. An assembly may contain one or more namespaces. Also, a namespace may contain types in more than one assembly.
// is used for single line comments; /* */ is used to enclose multi-line comments; and /// is used to create XML comments that can be exported to a text file.
A break statement causes control to exit the enclosing loop immediately; a continue statement continues the same loop by skipping to the beginning of the loop.
Each time concatenation occurs, a new string is created in memory. For a large number of concatenations, this wastes memory and also degrades performance. The StringBuilder class can be used as an alternative—but only where there are many concatenations. Otherwise, it will not outperform concatenation.
(a) Compilation error indicating that keyword new is required on Child.amethod(int, string) because it hides the inherited method. If new is used, the code prints “Base Constructor”.
The advantages of a class factory pattern are that it can control the number of objects created, encapsulates the logic required to create an object, and makes it easier to add new products by isolating the code in the factory.
Custom exceptions should inherit from the ApplicationException class. Three constructors should be included: a parameterless one, a constructor accepting a string parameter, and a constructor that accepts a string and Exception object parameter.
Generics permit a collection to be type-safe. This means the collection class is restricted to holding and processing objects of one type. Non-generic collections can hold any mixture of objects and require casting to detect their type.
The IDisposable interface is a convenient way to notify a client that an object's Dispose method should be called. The client only needs to check for the existence of the interface.
Objects with a Finalize method implemented are placed in a special queue so that Finalize can be executed before Garbage Collection occurs. This delays the actual Garbage Collection by one cycle.
Use the instance method when the expression is used repeatedly, because, unlike the static approach, it does not have to recompile the expression each time.
Docking attaches a control to an edge of its container; anchoring places a control in a fixed position relative to the edge(s) of a container. As the container is resized, the control remains a fixed distance from the edges.
The Form.TransparencyKey permits part of a form to appear transparent. Any part of the Form have the color assigned to this property becomes transparent. For example, to set the red portion of myForm to red, set the TransparencyKey as follows:
A MouseOver displays information associated with a control's ToolTip. Help text is enabled by pressing F1 or selecting the Help button and clicking a control to show context-sensitive Help.
To preview a document on the screen, you must create a PrintPreviewDialog object, set its Document property to the PrintDocument, and then call ShowDialog to display the preview:
PrintPreviewDialog prvDialog = new PrintPreviewDialog();
PrvDialog.Document = pd;
PrvDialog.ShowDialog(); // Show preview
Two validation techniques were discussed in this chapter. One approach is to use the XmlReader class directly by setting the Schemas and XsdValidate properties to enable validation. The other approach is to use the XmlValidatingReader class.
The DataReader remains connected to a data source and returns data in a forward, read-only cursor. A DataAdapter is a bridge between a data source and an internal data store. It typically loads data from a data source into a DataSet. It can also update the data source.
Simple binding occurs on a control that displays a single value; complex binding associates a control with a collection of data in a data source. In one-way data binding, the control is bound to the source for read-only purposes. Changes to the control's value are not reflected in the data source. Two-way binding permits the data source to be updated by changing the control's value(s).
b. It prints "Primary Thread" followed by "Worker Thread". The mutex in the main thread is created with ownership. The spawned thread's mutex must wait for the original one to finish before its thread continues processing.
There are many solutions. Here is one based on not grabbing a chopstick until both are available:
using System;
using System.Threading;
class Stick {
//Sticks available are designated as true
bool[] chopStick = {true, true, true, true, true};
// Attempt to pick up left and right chopstick
public void GetSticks(int left, int right)
{
lock (this)
{
// Release lock and wait until both chopsticks are free
while (!chopStick[left] && !chopStick[right])
Monitor.Wait(this);
chopStick[right] = false; chopStick[left] = false;
}
}
// Put chopsticks down
public void FreeSticks(int left, int right)
{
lock(this)
{
chopStick[right] = true;
chopStick[left] = true;
// Signal threads in queue that chopsticks are available
Monitor.PulseAll(this);
}
}
}
class Philosopher
{
int n; // Philosopher number
int eatDelay;
int thinkDelay;
int left, right;
Stick chopSticks;
public Philosopher (int n, int thinkTime,int eatTime,
Stick sticks)
{
this.n = n;
this.eatDelay = eatTime;
this.thinkDelay = thinkTime;
this.chopSticks = sticks;
// Fifth philosopher has chopstick 1 on left
left = n == 5 ? 1 : n+1;
right = n;
new Thread(new ThreadStart(Run)).Start();
}
public void Run()
{
while(true)
{
try
{
// Philosopher thinks for random amount of time
Thread.Sleep(thinkDelay);
chopSticks.GetSticks(left-1, right-1);
Console.WriteLine("Philosopher {0} is eating for
[1} ms ",n, eatDelay);
Thread.Sleep(eatDelay);
chopSticks.FreeSticks(left-1, right-1);
} catch { return; }
}
}
} // End of class Philosopher
public class Diners
{
public static void Main()
{
Stick sticks = new Stick();
// Create thread for each philosopher
// Eat time is random
Random r = new Random(DateTime.Now.Millisecond);
new Philosopher(1, 100, r.Next(500), sticks);
new Philosopher(2, 200, r.Next(500), sticks);
new Philosopher(3, 300, r.Next(500), sticks);
new Philosopher(4, 400, r.Next(500), sticks);
new Philosopher(5, 500, r.Next(500), sticks);
}
}
Single call server activation creates a new object on each call; single call singleton creates an object on the first call only. The object is then used for other calls.
Channel registration indicates the type of protocol to be used—usually TCP or HTTP—in the remoting and the port number to be used. Type registration specifies the available remote object and the activation mode type.
An interface or SoapSuds is used to prevent having to place a full assembly on the remoting client's machine. The assembly provides metadata, but can also expose proprietary code. An interface provides only the metadata a client requires. SoapSuds extracts the metadata and places it in a file that is deployed on the client's machine.
The IStackWalk interface defines the Assert, Deny, and PermitOnly methods to modify a stack walk. Assert stops the walk; Deny specifies permissions that cause the walk to fail; and PermitOnly specifies the only permissions that do not cause the stack walk to fail..
Delayed signing refers to using a public key to create a digital signature, as opposed to using the private key. When the final assembly is ready, the private key replaces the public key for encryption. This reduces the need to expose the private key during development.
A predefined permission is an individual permission class provided by .NET. A named permission set is a predefined set of permission objects. A security zone classifies where an assembly originates: Internet, Intranet, MyComputer, NoZone, Trusted, Untrusted.
If the TextBox.AutoPostBack property is set to true, the event handler is called immediately on the server; otherwise, it waits until the next round trip to the server occurs.
pageOutput in the web.config file specifies whether the trace log is appended to the Web page for display on the client's browser. If set to false, the output can be viewed in the trace.axd file in the application's root.
A data cache is read-only versus read/write for application state data. However, a data cache is much more flexible: An expiration can be assigned to it, and it can be tied to other objects, causing the cache data to be removed if the objects' value changes.
ASP.NET periodically invokes a resource scavenging process that removes less important data from a data cache. Importance is determined by priority. Thus, setting a high priority reduces the chance of the data cache being removed.
As a request or response passes through the HTTP pipeline, an Application object fires a series of events that may be handled by an HTTP module or in the global.asax file.
WebService attribute is applied to a class providing a Web Service. It describes the Web Service. WebService directive identifies a file as containing a Web Service.
BeginInvoke with polling using the IsCompleted property to check for a response. BeginInvoke with EndInvoke to retrieve results. Call <web service name>Async and implement event handler to process response.