Other types
The application of the .NET 2.0 basic class library is of course not limited to the font collection and Functional Programming. The range class below also has its clear design purpose and use.
Array
In .NET 2.0, the Array class expands support for norm programming. Of course, the Array class is not a model class (for compatibility consideration), but provides a series of support modes. In addition to the support of the previously mentioned Functional Programming, the Array class also provides a corresponding model version of the previous Object-based approach, so that the value type can improve the performance of finding and sorting. E.g:
Static Int Indexof
(T [] array, t value);
Static void sort
(T [] array);
In addition, some new model methods have been added, for example:
Static IList
Asreadonly
(T [] array); //
Returns a read-only list
Static void Resize
(Ref t [] array, int newsize; //
Change the array size
Another good news is that in .NET 2.0, the array will support the range collection interface. We know that before .NET 2.0, the Array abstraction class implements ILIST, ICOLLECTION, and IENUMERABLE port so that we can introduce these interfaces to an array. In .NET 2.0, the model set needs to use the Ienumerable
Such a model interface, so an array will also support these model interfaces. However, these model interfaces are not implemented in the Array class (because the Array class is not a model class), but is implemented by the CLR at runtime. For example, for int [], you can understand its implementation as follows:
Class Int []: array, list
, ICollection
, Ienumerable
ArraySegment
ArraySegment
Represents a parameter in the array. We know that the C # / CLR does not provide the feature of the default parameter, but requires the function overload. Therefore, there are a large number of heavy-duty methods for array parameters (indexing, length) (for convenience of caller), for example:
Class encoding {
Public Virtual Byte [] getBytes (char [] chars;
Public Virtual Byte [] getBytes (char [] Chars, int index, int count
...
}
For the designer of the class, there is more than a much more troublesome, and these overload methods actually correspond to the same implementation. In addition, there are many virtual functions to design so many virtual functions, which gives a lot of trouble, especially when these functions are Abstract.
In .NET 2.0, Microsoft attempts to provide ArraySegment
Class to solve this design problem. Use ArraySegment
If you want to design a method now, you only need to design a method, namely: (Note that this is not the real code of .NET 2.0, just explain the problem)
Class encoding {
Public Virtual Byte [] getBytes (arraysegment
...
}
And by the caller to determine how to pass an array parameter, for example:
Char [] chars = ...
Byte [] bytes = enc.getbytes (New ArraySegment
CHARS));
or
Byte [] bytes = enc.getbytes (New ArraySegment
(CHARS, 0, 10));
It can be seen, use Arraysegment
The disadvantage is that some code is written to the user. It may be this reason, so there is no official start to use it in .NET. Another reason may be considered for consistently considering the designed design.
Nullable
Nullable
The value type is used to represent a value that may be invalid or does not exist (this class is originally named Optionalue
). For example, some fields may be optional in the database design, and the designer of the data access interface can use Nullable
To return to the database field. Nullable
There are two read-only instance properties HasValue and Value. The former is a BOOL type for identifying whether it is valid, and the latter is T type data. Before accessing value, you must first determine if Hasvalue is TRUE, otherwise an exception will be thrown.
Nullable
Usually used for value types (such as Nullable
Because of the reference type, NULL itself can represent invalid status, in this case NULLABLE
There is not too much meaning.
It is worth mentioning that C # 2.0 is Nullable
Type provides a very simple and beautiful grammar, that is, INT? Is equal to NULLABLE
. This makes Nullable
It is very easy to use in C # and natural (after all the template looks like it. For example, the following code example:
INT? a = null; // a is empty (ie Hasvalue property is false)
INT? b = 10; // b is 10
In the future, when the design may return the validity API, we can also use Nullable
,E.g:
INT PARSENUMBER (String S); // Using anomalies
INT? TryParsenumber (String S); // Do not use unusual, use Nullable
EventHandler
The definition of an event and uses all corners of the .NET Framework. In the absence of a model, each event is delegated separately, for example:
Delegate Void EventHandler (Object Sender, Eventargs E);
Delegate Void KeyeventHandler (Object Sender, Keyeventargs E);
Such a disadvantage is that every time you define a new event commission, and you should learn more and a new event for users. In .NET 2.0, EventHandler introduced
The modest event commissioned to solve this problem, its prototype is as follows (note it in system.collections.generic namespace): delegate void EventHandler
(Object sender, t e) where t: Eventargs;
Use EventHandler
If you don't need to define your new event, you only need to provide your own event parameter class (you need to derive from Eventargs). One aspect is better, which is better (regardless of event definitions or users), and from the perspective of the CLR, because this type is entrusted to compile, all T type can only correspond to one binary implementation, so the system will improve the system Overall performance. Therefore, in Microsoft's latest design guidelines, it is recommended to entrust EventHandler
. Use EventHandler
The code example is, for example,:
Class myeventargs: Eventargs {
...
}
Class foo {
Public Event EventHandler
Myevent;
...
}
Foo foo = new foo ();
Foo.Myevent = New EventHandler
this.myeventhandler;
...