Approaches to Web Development for Bioinformatics

Developing Applications with C#

Previous  Contents  Next 
References

More on Objects, Classes, and Structs

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.


C#

// A class to hold information about symbols
public struct Symbol
{
public string name, annotation;

public Symbol(string _name, string _annotation)
{
name = _name;
annotation = _annotation;
}
}

class SymbolTest
{
static void Main()
{
Symbol a = new Symbol("a", "adenine");
Symbol g = new Symbol("g", "guanine");
Symbol c = new Symbol("c", "cytosine");
Symbol t = new Symbol("t", "thymine");

Symbol[] sequence = new Symbol[] { a, t, g, g, c, a, c, a, g, g, c, a };
for (int i=0; i<sequence.Length; i++)
{
System.Console.WriteLine(sequence[i].name + ": " + sequence[i].annotation);
}
}

}

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.


C#

// Class to read sequence data in any format
public abstract class SequenceReader
{
public abstract string loadSequence(string fileName);
}

// Class to read sequence data in FASTA format
public class FASTASequenceReader : SequenceReader
{
public override string loadSequence(string fileName)
{
// Read a FASTA format file from disk
// ...
// Dummy return data
return "atggcacaggca";
}
}

// Test to exercize the FASTASequenceReader class
class FASTASequenceReaderTest
{
static void Main()
{
SequenceReader sequenceReader = new FASTASequenceReader();
string sequence = sequenceReader.loadSequence("my_file.txt");
System.Console.WriteLine("Loaded sequence: " + sequence);
}
}

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,


C#

public partial class MyClass
{
...


Previous  Contents  Next 
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies