Everything in C# is an object and inherits from Object. Objects are instantiated from templates
defined by classes and structs. Objects contain properties to manage the information they encapsulate. They have
methods and events that allow them to perform actions. Classes are defined using the class keyword,
as in the GeneInfo example above. The class keyword may be preceded by an access modifier.
The public modifier means that the class make be used from anywhere. An object instance is
created from the class definition using the new keyword. Classes can contain fields, properties,
and methods.
Structs share most of the capabilities as classes but are more limited. They are intended for use as by objects for holding and grouping data. A struct cannot inherit from classes or other structs. Structs are value types as opposed to classes, which are reference types. When assigning a struct to a variable all of the data is copied, i.e. structs do not use references.
The program below illustrates structs. You may want to use something like this to associate some information with bioinformatics symbols. For example, if a user moused over a symbol, the web page could display the annotation. The use of structs here, however, is a wasteful use because the same data will be copied many times. The source is in Symbol.cs.
The program defines a struct to hold the name and annotation of the symbol. However,
when the Symbol objects are added to the array many copies are created - one for each
entry in the array. If a class ere used to define Symbol instead then every array entry
would consist of a reference to one of the four Symbol objects.
Classes can be declared as abstract. The point of this is to define some common base behavior of
a class. Abstract classes cannot be instantiated. It is a similar idea to the interface but
allows the definition of some concrete behavior in addition to abstract methods. Here
is an example that defines an abstract.
If you write a concrete class but intend derived classes to override its methods the base class
methods should use the virtual modifier and the derived class methods should use the
override modifier. This behavior is more like C++ than Java because, without the
virtual the actual method called depends on the type that the instance is declared as.
See the Polymorphism section in the C# Programmming
Guide43 for details.
In contrast the the abstract keyword the sealed keyword appearing before
a class means that the class cannot be derived from. This is similar to the final
modifier in Java. Use of this modifier is a good practice when publishing API's and you classes
are meant to be used as is, not overridden. The sealed modifier can also be used
for methods.
The Validator interface above demonstrated the use of an interface
to define the signature of a class. However,
interfaces in C# can be used to define methods, properties, events, and indexers for both
classes and structs.
Access modifiers include the keywords public, private, protected,
and internal. They apply to classes, interfaces, and structs and to class members.
Unless they are nested Classes and structs can be declared as either public or
internal. Public types can be accessed by any other type. Internal types can only be
accessed within the same assembly. If the keyword public is ommitted then the class or
struct is defaulted to internal.
Fields and methods can be public, internal, protected, or
private. If a member is declared as internal it is only available to inheriting types.
Private members can only be access within the class or struct declaring it.
A class, interface, or struct may be split over several source files. This can be an advantage when working
with automatically generated code to prevent your hand coded additions from being destroyed when regenerating
the code. To use this approach use the partial keyword. For example,
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.