1. The first C # program: the classic routine Hello World
"Hello World" can be said to be the first routine to learn every programming language. You can enter the following C # code in any editor, such as NotePad and Wordpad, save it as helloworld. cs, and run csc helloworld. cs in the command line to run the file:
// Using system
Using System;
Class Hello
{
Static void Main () {// display output on console
Console. WriteLine ("Hello, C # World! ");
}
}
2. Use the OpenFileDialog class to browse or open a file
Similar to the CfileDialog Open method in VC ++, The OpenFileDialog class in C # can be used to Open a file. This class is derived from FileDialog. Open a file using the OpenFile method in this class, and then you can read the file through steam.
Take a look at the following routine code, which uses the OpenFileDialog class to browse a file:
OpenFileDialog fdlg = new OpenFileDialog ();
Fdlg. Title = "C # Corner Open File Dialog ";
Fdlg. InitialDirectory = @ "c :";
Fdlg. Filter = "All files (*. *) | *. * | All files (*. *) | *.*";
Fdlg. FilterIndex = 2;
Fdlg. RestoreDirectory = true;
If (fdlg. ShowDialog () = DialogResult. OK)
{
TextBox1.Text = fdlg. FileName;
}
One row of Title sets the Title of the open dialog box. one row of Filter sets a Filter for the open file type, and one row of FileName contains the selected file name.
Is the result of running:
3. Call the COM component from C # (post-connection)
The. NET Framework is a natural development of COM. The two share many core elements, including Component Reuse and language neutrality. For backward compatibility, COM Interop can use existing COM components instead of modifying the original components. When a. NET Framework developer wants to merge COM code into a management application, he can use the COM Interop function to introduce the relevant COM type. After the introduction, this COM type can be used. This is a preliminary connection. However, sometimes you need a later connection of the object, which can also be implemented in. NET. You can use namespace ing to call the COM object through the later connection.
This section describes an application routine, which calls Excel and visualizes it by using a later connection.
Later, the connection will use the Reflectionb Type class. This Type class has many methods to obtain COM objects, just like the GetTypeFromProgID ("Application") We have used "), this method obtains the com id from the system registry, and then uses the STATIC class member Activator. createInstance () creates a new example of this COM object.
To call methods, functions, and attributes of a COM object, you must use the InvokeMethod () method that contains the correctly set Type object. This method accepts some parameter variables. The most important one is the ex attribute (get or set) of the method type ). In this example, we use the set attribute for Excel. Visible to visualize the Excel application.
We will try to call the Excel application in the. NET environment. This is a later connection application, because if it is a preliminary connection, you need to use the RCW (RunTime Callable Wraper: RunTime Callable package) of the COM object) to complete the tasks completed by the following command line program tblimp:
Ex. c:> tblimp/out:
Download comindotnet.zip, a console application. The following code calls excel:
File: // Variable
Type excel;
Object [] parameter = new object [1];
Object excelObject;
Try
{
File: // GetThe excel object
Excel = Type. GetTypeFromProgID ("Excel. Application ");
File: // CreateInstance of excel
ExcelObject = Activator. CreateInstance (excel );
File: // SetThe parameter whic u want to set
Parameter [0] = true;
File: // SetThe Visible property
Excel. InvokeMember ("Visible", BindingFlags. SetProperty, null, excelObject, parameter );
}
Catch (Exception e)
{
Console. WriteLine ("Error Stack {0}", e. Message );
}
Finally
{
File: // WhenThis object is destroyed the Excel application will be closed
File: // SoSleep for sometime and see the excel application
Thread. Sleep (5000 );
File: // RelaeseThe object
File: // GC. RunFinalizers()
}
4. Create a multi-threaded Application
It is very easy to compile a multi-threaded application in. NET and C. Even for beginners who have never used C # To Write multi-threaded applications, follow these simple steps.
Define namespace
In. NET, The multithreading function is defined in the System. Threading namespace. Therefore, before using any thread class, you must define the System. Threading namespace. The definition method is as follows:
Using System. Threading;
Start thread
The Thread class in the System. threading namespace represents a Thread object. This class object can be used to create new threads, delete, pause, and recover threads. The following code uses the Thread class to create a new Thread and then start the Thread:
Thread = new Thread (new ThreadStart (WriteData ));
Thread. Start ();
WriteData is a function to be executed by this thread. The Code is as follows:
Protected void WriteData ()
{
String str;
For (int I = 0; I <= 10000; I ++)
{
Str = "Secondary Thread" + I. ToString ();
Console. WriteLine (listView1.ListItems. Count, str, 0, new string [] {""});
Update ();
}
}
Kill thread
The Abort method of the Thread class is used to permanently kill a Thread. However, before calling the Abort method, you must determine whether the thread is still activated, that is, to determine the value of thread. IsAlive:
If (thread. IsAlive)
{
Thread. Abort ();
}
Pause a thread
The Thread. Sleep method is used to pause a Thread for a period of time. The Code is as follows:
Thread. Sleep ();
Set thread priority
We can use the ThreadPriority attribute of the Thread class to set the Thread priority. The value range of thread priority is Normal, AboveNormal, BelowNormal, Highest, or Lowest. See the following configuration code:
Thread. Priority = ThreadPriority. Highest;
Delay thread
The Suspend method of the Thread class can delay a Thread. The thread is delayed until the Resume method is called.
If (thread. ThreadState = ThreadState. Running)
{
Thread. Suspend ();
}
Restore delayed threads
Call the Resume method to restore a delayed thread. If the thread is not delayed, the Resume method is invalid.
If (thread. ThreadState = ThreadState. Suspended)
{
Thread. Resume ();
}