C # development-generic, file

Source: Internet
Author: User

The objective of generics is to use a wide range of applicable and interactive forms to represent algorithms and data structures-parameterization

The wildcard can provide powerful type checks during compilation to reduce Explicit conversions between data types, and check the packing operations and runtime types.

Generic Type parameter t can be considered as a placeholder, representing a possible type

Namespace test01 {// create a generic interface public interface igenericinterface <t> {T createinstance (); // call the createinstance method in the interface} // implement the generic class of the above generic interface // The derived constraint where T: Ti (T must inherit from Ti) // constructor constraint where T: New () (t can be instantiated) public class factory <t, Ti>: igenericinterface <Ti> where T: Ti, new () {public Ti createinstance () // create a public method createinstance {return New T () ;}} class program {static void main (string [] ARGs) {// instantiate the interface igenericinterface <system. componentmodel. ilistsource> factory = new factory <system. data. datatable, system. componentmodel. ilistsource> (); // outputs the console of the specified generic type. writeline (factory. createinstance (). getType (). tostring (); console. readline ();}}}

When instantiating a generic type, you can use constraints to restrict the type types of type parameters. constraints are specified using the where context keyword.

 

Generic method, including the type parameter T method in the Declaration

The generic method can be overloaded with multiple types of parameters.

Namespace test02 {public class finder // create a public class finder {public static int find <t> (T [] items, t item) // create a generic method {for (INT I = 0; I <items. length; I ++) // call the for loop {If (items [I]. equals (item) // call the equals method to compare two numbers {return I; // return the equal number position in the array} return-1; // if the specified number does not exist, -1} class program {static void main (string [] ARGs) {int I = finder is returned. find <int> (New int [] {1, 2, 3, 4, 5, 6, 8, 9}, 6); // call the generic method, and define the array to specify the number console. writeline ("6 position in the array:" + I. tostring (); // The Position of the number in the output in the array console. readline ();}}}
namespace Test03{    class Program    {        static void Main(string[] args)        {            List<int> myList = new List<int>();            for (int i = 0; i < 10; i++)            {                myList.Add(i);            }            foreach (int i in myList)            {                Console.WriteLine(i);            }            Console.ReadLine();        }    }}
Namespace test04 {class program {static void main (string [] ARGs) {myclass1 <int> mclass1 = new myclass1 <int> (); myclass2 <int> mclass2 = new myclass2 <int> (); console. readline ();} class myclass1 <t> {public myclass1 () {console. writeline ("This is the first generic class");} class myclass2 <t>: myclass1 <t> {public myclass2 () {console. writeline ("this is the second generic class ");}}}}

System. Io namespace

The file class and Directory class are used to operate files and directories respectively. They can be instantiated, but cannot be inherited by other classes.

All methods in the file class are static. Therefore, if you only want to execute one operation, the efficiency of using methods in the file class is higher than that in the corresponding fileinfo class.

The static method of the file class performs a security check on all methods. If you want to reuse an object multiple times, consider the fileinfo method-do not always check

If (file. exists (textbox1.text) // use the exists method of the file class to determine whether the file to be created exists {MessageBox. show ("this file already exists");} else {file. create (textbox1.text); // use the Create method of the file class to create a file}

Directory class, Operation directory

If (directory. exists (textbox1.text) // use the exists method of the Directory class to determine whether the folder to be created exists {MessageBox. show ("this folder already exists");} else {directory. createdirectory (textbox1.text); // use the createdirectory method of the Directory class to create a folder}

The fileinfo class does not have static methods. The methods in this class can be used to instantiate objects.

If you want to perform several operations (or repeated operations) on the file, it is more efficient to instantiate the fileinfo object.

Fileinfo finfo = new fileinfo (textbox1.text); If (finfo. exists) // use the exists attribute of the fileinfo object to determine whether the file to be created exists {MessageBox. show ("this file already exists");} else {finfo. create (); // use the create method of the fileinfo object to create a file}
Directoryinfo dinfo = new directoryinfo (textbox1.text); // instantiate the directoryinfo class object if (dinfo. exists) // use the exists attribute of the directoryinfo object to determine whether the folder to be created exists {MessageBox. show ("this folder already exists");} else {dinfo. create (); // use the create method of the directoryinfo object to create a folder}

Get File Information

If (openfiledialog1.showdialog () = dialogresult. OK) {textbox1.text = complete; fileinfo finfo = new fileinfo (textbox1.text); // instantiate the fileinfo object string strctime, strlatime, strlwtime, strname, strfname, strdname, strisread; long lglength; strctime = finfo. creationtime. tow.datestring (); // obtain the File Creation Time strlatime = finfo. lastaccesstime. tow.datestring (); // obtain the last time the file was accessed. strlwtime = finfo. lastwritetime. tow.datestring (); // obtain the time when the last file was written. strname = finfo. name; // obtain the file name strfname = finfo. fullname; // obtain the complete file directory strdname = finfo. directoryname; // obtain the complete file path strisread = finfo. isreadonly. tostring (); // obtain whether the object is read-only. length; // get the file length MessageBox. show ("File Information: \ n creation time:" + strctime + "last access time:" + strlatime + "\ n last write time:" + strlwtime + "File Name: "+ strname +" \ n full directory: "+ strfname +" \ n full path: "+ strdname +" \ n read-only: "+ strisread +" file length: "+ lglength );}

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.