Tips for seven C # programming

Source: Internet
Author: User
Tags foreach array bool command line interface serialization client
Programming | skills

One, minimized window

When you click on "X" or "alt+f4", minimize the window,
Such as:
protected override void WndProc (ref message M)
{
const int wm_syscommand = 0x0112;
const int sc_close = 0xf060;
if (m.msg = = Wm_syscommand && (int) M.wparam = = Sc_close)
{
User clicked Close button
This. WindowState = formwindowstate.minimized;
Return
}
Base. WndProc (ref m);
}

Ii. How to make the Foreach Loop run faster
foreach is a ready-made statement that simply enumerates and processes elements in a collection, as shown in the following example:
Using System;
Using System.Collections;
Namespace Looptest
{
Class Class1
{
static void Main (string[] args)
{
Create an ArrayList of strings
ArrayList array = new ArrayList ();
Array. ADD ("Marty");
Array. ADD ("Bill");
Array. ADD ("George");
Print the value of every item
foreach (string item in array)
{
Console.WriteLine (item);
}
}
}
You can use the foreach statement in each collection that implements the IEnumerable interface. If you want to learn more about foreach usage, you can view the C # Language specification in the. NET Framework SDK documentation.

At compile time, the C # Editor transforms each foreach region. IEnumerator enumerator = array. GetEnumerator ();
Try
{
string item;
while (enumerator. MoveNext ())
{
item = (string) Enumerator. Current;
Console.WriteLine (item);
}
}
Finally
{
IDisposable d = enumerator as IDisposable;
if (d!= null) D.dispose ();
}
This means that in the background, foreach management will give your program extra code to increase the overhead of the system.

Third, save the picture to an XML file
WinForm's resource file, the PictureBox image property, and so on, are converted to text saving, which is accomplished by serialization (serialization).
Example://
Using System.Runtime.Serialization.Formatters.Soap;
Stream stream = new FileStream ("E:\\image.xml", Filemode.create,fileaccess.write,fileshare.none);
SoapFormatter f = new SoapFormatter ();
Image img = image.fromfile ("E:\\image.bmp");
F.serialize (STREAM,IMG);
Stream. Close ();

Four, shielding Ctrl-v
The TextBox control in WinForm has no way to mask the ctrl-v clipboard paste action, if you need an input box, but you do not want the user to paste the contents of the Clipboard, you can use the RichTextBox control instead, and the CTRL-V key is masked in KeyDown, example:

private void Richtextbox1_keydown (object sender, System.Windows.Forms.KeyEventArgs e)
{
if (E.control && e.keycode==keys.v)
E.handled = true;
}

V. Determine whether a file or folder exists
With System.IO.File, it is easy to check whether a file exists:
BOOL exist = System.IO.File.Exists (FileName);

If you need to determine whether a directory (folder) exists, you can use System.IO.Directory:
BOOL exist = System.IO.Directory.Exists (FolderName);

Vi. Designing custom events using the delegate type
In C # programming, any class can have its own events (event), except for method and property. The steps for defining and using custom events are as follows:
(1) Define a delegate type outside of class to determine the interface of the event program
(2) Within class, declare a public event variable, type delegate type defined by the previous step
(3) to trigger an event somewhere within a method or property
(4) The client program uses the + + operator to specify the event handler

Example://define delegate type, constrain event program parameters
public delegate void MyEventHandler (object sender, long linenumber);

public class Dataimports
{
Define new event Newlineread
public event MyEventHandler Newlineread;

public void ImportData ()
{
Long i = 0; Event arguments
while ()
{
i++;
Triggering events
if (newlineread!= null) newlineread (this, i);
//...
}
//...
}
//...
}

The following is the client code

private void Callmethod ()
{
Declare class variable, no WithEvents required
Private Dataimports _da = null;
Specifying an event handler
_da. Newlineread + = new MyEventHandler (this.da_enternewline);
Calling the class method, which triggers an event on the way
_da. ImportData ();
}
Event handlers
private void Da_enternewline (object sender, long linenumber)
{
// ...
}

vii. IP and host name resolution
Use System.Net to implement IP resolution functions similar to the ping command line, such as resolving host names to IP or vice versa: private string Gethostnamebyip (string ipaddress)
{
Iphostentry hostinfo = dns.gethostbyaddress (ipaddress);
return hostinfo.hostname;
}
private string Getipbyhostname (String hostName)
{
System.Net.IPHostEntry hostinfo = Dns.gethostbyname (hostName);
Return hostinfo.addresslist[0]. ToString ();
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.