C# is a strongly typed language. This is contrary to scripting languages like Perl, PHP, and JavaScript where the type is determined internally by the interpreter and it can change dynamically. Data types in C# are either value types, which store the values or reference types, which store references to data. Value types include numeric types, Boolean types, and struct types. Assigning one value type to another value type copies the data.
A reference type can be either a class, an interface, or a delegate.
What you might want to do in a web application is to get data from a database and process it in C#. Having done that you might pass this to an ASP page to present the results. The kind of class that you might need in carrying data from the database to the web page might look something like GeneInfo in the example below. There is also a test class to exercise the GeneInfo class printing the gene information out to the console. The source code is in GeneInfo.cs.
This example demonstrates the class fields (id, symbol, and name).
These values are set in the constructor, a method with the same name as the class used to initialize
an instance of the class.
The method ToString() is defined to help in printing out the field values. The base
ToString() method is defined on Object to provide a convenient method
to convert a value to a string. This also demonstrates the use of the override keyword, which is
a feature that Java does not have. If the override keyword were not used I would not get the
behavior I am looking for - a convenient method to print out information about the object.
In C# inheritance is specified via an : operator in the class definition. For example,
public class Tiger:Animal means that the class Tiger extends the class
Animal. I do not have much use for inheritance but you will see it in examples elsewhere in the
literature.
Interfaces specify the methods and properties of classes.
Implementation of C# interfaces is also specified via an : operator.
A delegate is a reference type used to encapsulate a named or anonymous method. They are similar to function pointers in other languages but they have a type associated with them. They are used extensively for events in C#.
C# has built-in reference types for object and string. A variable of type object can take the value
of from a value type. For example,
In this case, the variable start is said to be "boxed".
A string is a sequence of Unicode characters. The == operator compares strings
and the + concatenates them. Individual characters are accessed with the
[] operator.
Here is an example that demonstrates a few of the concepts just discussed.
The code is in file RNAValidator.cs.
This program defines a Validator interface. The intent of this interface is to check
whether the given string is valid from some, unspecified point of view. This could be used, for
example, in a web form where we want to validate user input but don't want to tie the presentation
logic to the science logic. The RNAValidator class implements the Validator
interface to check whether the given string is a valid RNA string.
Starting execution in the Main method the program the program declares a variable
validator of type Validator. Notice the declaration using the interface.
The program then instantiates an object of type RNAValidator. If we truly wanted
the code making use of the Validator to remain oblivious of the implementation we
would use some kind of plug-in or factory pattern to instantiate it somewhere else.
The idea of iterating through each character in a string and checking its validity with an if statement is tedious and will not work for more complex examples. Let's look at a better approach using regular expressions.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.