Reread C # InDepth (1)

Source: Internet
Author: User

Reread C # InDepth (1)
A good book, or a deep book, is a new discovery every time you study it. Okay, I admit that every time I read it, there is a general suspicion that it is always possible ~~ Over the past few years, I have been focusing on the development of the C # client. Gradually, I am confused, confused, and self-righteous. Finally, I am down to review it. Maybe this is also a step-by-step process of self-learning. I have mentioned so many things before, and I hope that you will not be so impetuous and "deeply understand" C # Every knowledge point in such a language. This article summarizes the knowledge in books and provides an overview based on actual application scenarios. If there are any errors, please kindly advise. The content in this article is relatively simple. Please skip this article. 1. the simplified COM operation copies the code private void Button_Click (object sender, RoutedEventArgs e) {var Product = new List <Good> (); Product. add (new Good () {Name = "Tom", Age = 21}); Product. add (new Good () {Name = "Json", Age = 22}); Product. add (new Good () {Name = "Jacob", Age = 26}); var app = new Microsoft. office. interop. excel. application () {Visible = false}; Workbook wb = app. workbooks. add (); Worksheet ws = App. activeSheet; int row = 1; foreach (var good in Product) {ws. cells [row, 1]. value = good. name; ws. cells [row, 2]. value = good. age; // Dynamic C #4.0 syntax row ++;} wb. saveAs (Filename: PractiseDemoLib. util. rootPath + "Demo.xls", FileFormat: XlFileFormat. xlWorkbookNormal); app. application. quit () ;}copy the code copy code public class Good {public string Name {get; set ;}public Int32 Age {get; set ;}} copy the code process Introduce the Microsoft. Office. Interop. Excel component in sequence. If no, you can download the component or install the Excel component. This is an elegant expression of the C #4.0 syntax (in red), which avoids the previous implementation method. Dynamic syntax is not limited here, it has the "dynamic" advantage in reflection programming and interaction with other speech, which will be described later. 2. generic constraints copy code public class A <T> where T: class, IDisposable, new () {public string Name {get; set ;}} public class A <T, u> where T: class, IDisposable, new () where U: class, T {public string Name {get; set ;}} /** example of non-constraint * Class B <T>: where T: Object, System. enum, System. valueType, System. delegate ***/the generic replication code is used to solve the problem of packing and unpacking efficiency. In addition, the program is reused to a greater extent. The generic constraint is to constrain the type of the input type so that it should have a certain type of method or attribute. Note the following points: 1. type T can be restricted to class or interface type, but cannot be restricted to where T: Object, System. enum, System. valueType, System. delegate. 2. the construction of type T must be a non-argument Constructor (CLR does not have this constraint, so it can still be built in some ways, but not in IDE mode), that is, it is constrained to new T () mode, and new () is placed at the end of the constraint list. 3. Type T can be constrained to type U. 3. Static type nesting involves static types which need to be distinguished by static types and instance types, static structures and instance structures. Copy the public class Outer <T> {public class Inner <U, V> {readonly static int HashCode; static bool IsInit = false; static Inner () {HashCode = typeof (Outer <T> ). getHashCode ();} public static void DynamicMethod (object sender) {var win = sender as MainWindow; win. outPutMsg (string. format ("[{4}, {3}] Outer <{0}>. inner <{1 },{ 2}> ", typeof (T), typeof (U), typeof (V), HashCode. toString (), IsInit. toString ())); IsInit = true ;}} private void Button_Click (object sender, RoutedEventArgs e) {Outer <int>. inner <string, DateTime>. dynamicMethod (this); Outer <string>. inner <int, int>. dynamicMethod (this); Outer <object>. inner <string, int>. dynamicMethod (this); Outer <int>. inner <string, DateTime>. dynamicMethod (this);} copying code this example mainly demonstrates that the static constructor only initializes once, so that when you click Button_Click, only three objects will be initialized, because the first and fourth groups, the program considers that the input parameters are the same and only initializes one static structure. [False, 29514189] Outer <System. int32>. inner <System. string, System. dateTime & gt; [False, 53070131] Outer <System. string>. inner <System. int32, System. int32 & gt; [False, 39345664] Outer <System. object>. inner <System. string, System. int32> [True, 29514189] Outer <System. int32>. inner <System. string, System. dateTime> the above shows the result. You can compare the code.

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.