Example 188 Comparing loop to brute force

Return 1+2+3+4+5+6+7+8+9+10+11 +12+13+14+15+16+17+18+19+2 0 for (int i 0 i < iterations i++) display the results Console.WriteLine( Loop Sum of ( 0 ) 1 , val, result) Console.WriteLine( The elapsed time in milliseconds is + elapsed.TotalMilliseconds.ToString( )) mark a new start time startTime DateTime.Now for (int i 0 i < iterations i++) mark the new elapsed time elapsed DateTime.Now - startTime display the results Console.WriteLine( Brute Force Sum of ( 0 ) 1 , val, result)...

Application Domains

A process is, essentially, a running application. Each .NET application runs in its own process. If you have Word, Excel, and Visual Studio open, you have three processes running. If you open another copy of Word, another process starts up. Each process is subdivided into one or more application domains (or app domains). An app domain acts like a process but uses fewer resources. App domains can be independently started and halted they are secure, lightweight, and versatile. An app domain can...

The End of DLL Hell

You will remember this scenario you install Application A on your machine, and it loads a number of DLLs into your Windows directory. It works great for months. You then install Application B on your machine, and suddenly, unexpectedly, Application A breaks. Application B is in no way related to Application A. So what happened It turns out, you later learn, that Application B replaced a DLL that Application A needed, and suddenly Application A begins to...

Example 116 Working with an exception object

Test t new Test( ) t.TestFunc( ) try to divide two numbers handle possible exceptions Console.WriteLine(Open file here) double a 12 double b 0 Console.WriteLine ( 0 1 2 , a, b, DoDivide(a,b)) Console.WriteLine ( This line may or may not print) most derived exception type first catch (System.DivideByZeroException e) nDivideByZeroException Msg 0 , e.Message) nHelpLink 0 , e.HelpLink) nHere's a stack trace 0 n, e.StackTrace) Unknown exception caught) Console.WriteLine ( Close file here.) public...

Example 219 Implementing a network streaming client

Static public void Main string Args create a TcpClient to talk to the server TcpClient socketForServer new TcpClient localHost, 65000 Failed to connect to server at 0 65000, localhost return create the Network Stream and the Stream Reader object NetworkStream networkStream socketForServer.GetStream System.IO.StreamReader streamReader new read the data from the host and display it outputString streamReader.ReadLine Exception reading from Server To test this, I created a simple test file named...

Dynamic Invocation with Invoke Member

The first approach will be to create a class named BruteForceSums dynamically, at runtime. The BruteForceSums class will contain a method, ComputeSum , that implements the brute-force approach. You'll write that class to disk, compile it, and then use dynamic invocation to invoke its brute-force method by means of the InvokeMember method of the Type class. The key point is that BruteForceSums.cs won't exist until you run the program. You'll create it when you need it and supply its arguments...

Example 919 Using the IDictionary Enumerator interface

a simple class to store in the array Create and initialize a new Hashtable. Hashtable hashTable new Hashtable hashTable.Add 000440312, George Washington hashTable.Add 000123933, Abraham Lincoln hashTable.Add 000145938, John Galt hashTable.Add 000773394, Ayn Rand Display the properties and values of the Hashtable. Console.WriteLine hashTable Console.WriteLine Count 0 , hashTable.Count Console.WriteLine Keys and Values PrintKeysAndValues hashTable public static void PrintKeysAndValues Hashtable...

Example 2110 Implementing an asynchronous network streaming server

Public ClientHandler Socket socketForClient socket socketForClient buffer new byte 256 networkStream new AsyncCallback this.OnReadComplete new begin reading the string from the client buffer, 0, buffer.Length, callbackRead, null when called back by the read, display the string and echo it back to the client private void OnReadComplete IAsyncResult ar int bytesRead networkStream.EndRead ar buffer, 0, bytesRead Console.Write Received 0 bytes from client 1 , bytesRead, s networkStream.BeginWrite...