In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the methods of designing C# components". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the methods of C# component design"!
It is easy to predefine properties for a class in C #, see Program 1.
Program 1
Using System; namespace PropertiesDemo {public class MyData public class Class1 {private MyData _ data; public MyData Data {get {return _ data;}} public Class1 () {_ data = new MyData ();}
This is a fairly common way to predefine properties, and it is also a working program, but there is a design problem hidden in it, which is the timing of creating MyData objects. According to the technique of program 2-1, when the Class1 object is created, the _ data object in it is also created, which causes the Class1 object to pay the memory cost of a MyData object at the initial stage of creation, which may be silly for a simple class, but what if the Class1 object has a group of such properties? In order to solve these problems, Lazy-Allocate (slow allocation) technology is widely used in .NET Framework, see Program 2.
Program 2 Lazy-Allocate example
Public class Class1 {private MyData _ data; public MyData Data {get {if (_ data = = null) data = new MyData (); return _ data;}} public Class1 () {}}
The design concept of Lazy-Allocate is simple: no upfront cost before use. Compared with the concept of Pre-Allocate (pre-allocation) used in program 2-1, program 2-2 adopts the strategy of exchanging time for space and pays the price of access judgment to reduce the waste of space. Of course, Pre-Allocate is not useless, the quick access feature without prejudgment is applicable to the attributes that users are bound to access, but on some specific attributes, such as the Style attribute common in ASP.NET, it is not suitable to use Pre-Allocate techniques, because users may not necessarily use this attribute, in this case, Lazy-Allocate mode can save objects some memory costs.
Event
Event handling is a very important part of component design. In C #, events are closely related to delegate. Program 3 is a simple event example.
Program 3 simple event example
Using System; namespace EventDemo {public delegate void ProcessHandler (object sender); public class Class1 {private event ProcessHandler _ processHandler = null; public event ProcessHandler ProcessStart {add {_ processHandler + = value;} remove {_ processHandler-= value;}} public void Process () {_ processHandler (this); for (int I = 0; I < 10; iTun1;} public Class1 () {}
In C #, delegate acts as a function pointer. Users can add a function to a delegate, while a delegate allows users to join more than one function. When calling this delegate, it is equivalent to calling all the functions contained in it. However, there is a hidden problem with the design of program 2-3, that is, when there are a large number of events, the object must pay a corresponding number of delegate variables, as shown in program 4.
Program 4 traditional event design
Private event ProcessHandler _ processStart = null; private event ProcessHandler _ processEnd = null; private event ProcessHandler _ processStep = null; so far, I believe you have a deeper understanding of "what are the methods of designing C# components?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.