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. Simplified com operations
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(); }
public class Good { public string Name { get; set; } public Int32 Age { get; set; } }
The Microsoft. Office. InterOP. Excel component is introduced in the program. If no Microsoft. Office. InterOP. Excel component is available, you can download it or install Excel.
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
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 * Class B <t>: where T: object, system. enum, system. valuetype, system. delegate ***/
The emergence of generics is more to solve the problem of packing and unpacking efficiency, and the use of generics, 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
When it comes to static types, we need to differentiate static types and instance types, static structures and instance structures.
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); }
This example mainly demonstrates that the static constructor only initializes one time, 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>[False,53070131] Outer<System.String>.Inner<System.Int32,System.Int32>[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.
Continuous update: Sample Code download