Reusable libraries
Class Libraries
1. A class library is a good way to encapsulate a common set of logic so it can be shared among multiple application. This is useful both to promote reusability of the classes in the library and to keep the portions of an application down to a manageable size.
2. Classes in C# are very similar to classes in the C++ and Java languages.Classes are created with the class keyword,with their contents enclosed in braces. All classes implicitly inherit from the object class even when it is not specified.
3. Note that a semicolon(;) is not required after the class definition.
4. In Visual Studio, every assembly is typically a separate project within the solution.
5. Visual Studio automatically uses the project name as the namespace for all files in the project.
6. A property in C# is created much like a method. You define an access level, a type, and a name for the property. By Microsoft conventions,property names begin with a capital letter, and are noun or noun phrase.
7. The access methods provide read access,via the get keyword, or write access, via the set keyword.
The OBJECT CLASS
1. As we have repeatedly indicated, all classes in C# implicitly inherit from the object class, which is the same as System.Object class.
2. You should always(yes,always!) override GetHashCode if you are overriding the Equals method.
3. In c#, the override keyword is required to override a virtual method. The override keyword indicates that the Equals method here serves the same purpose as this base member. To define a new meaning for an inherited member and hide the original definition, the new modifier is used instead of the override keywork.
4. In Equal method, Since we must handle any given object here, we only perform our comparison if the given object is a Photograph, We use the is keyword for this purpose, which evaluates to true is the variable has the given type, and fasle otherwise.
5. The default String.Equals method performs a case-sensitive comparison fo strings. That is,”book” and “book” are equal, but”book” and “Book” are not. To ingnore capitalization in our filename strings, we use an alternate form of the String.Equals method that accepts a StringComparison enumeration value to indicate how the strings should be compared. The StringComparison.InvariantCultureIgnoreCse setting cause a culture-insensitive comparison to be performed that ignores character case.
Interfaces
1. An interface is an abstraction of an abstraction, and should be familiar to programmers of COM or its UNIX ancestor, the distributed computing environment(DCE). While a class encapsulates a data structure and its operations, an interface encapsulates a type of data structure and its operations. This can be confusing. Said another way: a class defines a set of operations, while an interface defines a set of operations that classes can support.
2. This is similar to an abstract class, except that an interface does not provide mentations of any members; it just defines the properties, methods, and events that a class must implement in order to support the interface. An interface encapsulates a common idea for use by unrelated classes, while an abstract class encapsulates a common idea for use by related classes.
Interfaces related to data collections
1. IEnumerable ---------Description: Interface that supports the creation of an enumerator class for iterating over the elements in a collection. Usage: Supporting this interface allows the C# foreach statement to be used with instances of a class or structure. Members : Get Enumerator method, which returns a class that supports the Ienumberator interface.
2. Iemumerator-----------Description: Interface for stepping through the elements in a collection. Members: Current Property, to retrieve the current element from the collection/ MoveNext method, which advances to the next element in the collection/Reset method, which sets the enumerator just before the first element
3. Icollection-------Description: An Ienumerable interface that provides sizing and synchronization capabilities. This interface is the basis for all collections in the .NET Framework. Members: Count property, to retrieve the number of elements in the collection/SyncRoot property, to retrieve an object for synchronizing multithreaded access to the collection./CopyTo method, which copies the elements in the colleciton into an Array object.
4. Ilist----------An Icollection interface that provides indexing of its elements. Usage: Supporting this interface allows a class or structure to be treated as an array. This permits objects to be used as targets of data bound controls, as discussed in chapter21. Member: Item property, to support array-style indexing of elements using[brackets], much like a [] override in C++/ Add method, which adds a new element to the collection/Contains method, which determines if the collection contains a specific object/Remove method, to remove the element from the collection at a given index value.
5. The CollectionBase class is an abstract class for creating strongly typed collections. A class is strongly typed if it only allows a specific type or types in its methods, rather than a general type such as object. Strongly typed classes allow the compiler to ensure that the proper objects are passed to methods in the class, and can prevent errors that would otherwise ocuur only at runtime.
Generics
1. One of the great benefits of object-oriented programming is the ability to encapsulate an object and reuse it in different applications.
2. Generics attempt to address this issue with a mechanism for defining generalized solutions and design patterns that are type-safe and efficient. Generic classes use angle brackets(<>) to define a generalized type that is referenced within the class.
3. The angle brackets indicate to the compiler that this is a generic class definition. By convention, a T is typically used to indicate the type parameter, as it is called.
4. The System.Collections.Generic namespace provides a rather extensive set of generic classes for supporting and manipulating type-safe collections in.NET.
5. The Collection<T> class is a base class for generic collections. This class provides an extensible IList implementation suitable for creating custom generic collections,and is part of the System.Collections.ObjectModel namespace. The Collection<T> class supports the Ilist, Icollection, and Ienumerable interface.
6. There are two primary uses for the base keyword. The first is as a placeholder in the constructor for invoking a constructor in the base class. The second use is to access a member of the base class from a derived class.
Disposing of resources
1. We have no idea when the garbage collector runs. It could be immediately, it could be hours later, or it even be when the program exits. This is fine for most application memory, but might present a problem for system resources.
2. The Idisposable interface indicates that an object can be disposed of. Instances of objects that support this interface should call the Dispose method to free system resources before the last reference to the object is discarded.
浙公网安备 33010602011771号