Other C Features


Other C# Features

You have now completed examining the code samples. The remainder of this appendix briefly examines a couple of features of C# that you need to be aware of when making the transition from Visual Basic to C#, and which haven't yet been discussed; in particular some of the C# concepts relating to data types and operators.

Data Types

As indicated, the data types available in C# do differ in detail from those available in Visual Basic. Furthermore, all C# data types have features that you would normally associate with an object. For example, every type, even simple types such as int and float, supports the calling of methods.(Incidentally, this feature does not cause any loss of performance.)

Although the types available in C# are slightly different from Visual Basic types, most of the types that you are familiar with in Visual Basic do have equivalents in C#. For example, the Visual Basic Double type translates to double in C#; The C# equivalent of the Date type is the .NET base class, DateTime, which implements a huge number of methods and properties to allow you to extract or set the date using different formats.

One exception, however, is Variant, for which there is no equivalent in C#. The Variant type is a very generic type, which to some extent exists only in order to support scripting languages that are not aware of any other data types. The philosophy of C#, however, is that the language is strongly typed. The idea is that if, at each point in the program, you have to indicate the data type you are referring to, at least one major source of runtime bugs is eliminated. Because of this, a Variant type isn't really appropriate to C#. However, there are still some situations in which you do need to refer to a variable without indicating what type that variable is, and for those cases C# does have the object type. C#'s object is analogous to Object in Visual Basic. However, Object specifically refers to a COM object, and therefore can only be used to refer to objects, which in Visual Basic terms means to reference data types. For example, you cannot use an object reference to refer to an Integer or to a Single type. In C#, by contrast, an object0 method can be used to refer to any .NET data type, and because all data types are .NET data types, this means that you can legitimately convert anything to an object, including int, float, and all the predefined data types. To this extent, object in C# does perform a similar role to Variant in Visual Basic.

Value and Reference Types

In Visual Basic there is a sharp distinction between value types and reference types. Value types include most of the predefined data types: Integer, Single, Double, and even Variant (though strictly speaking Variant can also contain a reference). Reference types are any object, including class modules that you define and ActiveX objects. As you will have noticed through the samples in this appendix, C# also makes the distinction between value and reference types. However, C# is more flexible to the extent that it permits you, when defining a class, to specify that that class should be a value type. You do this by declaring the class as something called a struct. As far as C# is concerned, a struct is basically a special type of class that is represented as a value rather than a reference. The overhead involved in instantiating structs and destroying them when you are finished with them is less than that involved when instantiating and destroying classes. However, C# does restrict the features supported by structs. In particular, you cannot derive classes or other structs from structs. The reasoning here is that structs are intended to be used for really lightweight, simple objects, for which inheritance isn't really appropriate. In fact, all the predefined classes in C#, such as int, long, float, and double are actually .NET structs, which is why you can call methods such as ToString() against them. The data type string, however, is a reference type and so is really just a class.

Operators

You need to know a couple things about operators in C#, because they do differ somewhat from Visual Basic operators, and this can catch you off guard if you are used to the Visual Basic way of working. In Visual Basic there are really two types of operators:

  • The assignment operator, =, which assigns values to variables

  • All the other operators, such as +, -,*, and /, which each return some value

There is an important distinction here in that none of the operators, apart from =, has any effect in terms of modifying any value. On the other hand, = assigns a value but does not return anything. There are no operators that do both.

In C#, this categorization simply does not exist. The rule in C# is that all operators return a value, and some operators also assign some value to a variable. You have already seen an example of this when you examined the addition assignment operator, +=:

 int A=5, B=15; A+=B; // performs an arithmetic operation AND assigns result (20) to A 

+= returns a value as well as assigning the value. It returns the new value that has been assigned. Because of this you could actually write:

 int A=5, B=15; int C = (A+=B); 

This will have the results that both A and C will be assigned the value 20. The assignment operator, =, also returns a value. It returns the value that has been assigned to the variable on the left side of the expression. This means that you can write code like this:

 C = (A = B); 

This code sets A equal to whatever value is in B, and then sets C to this same value too. You can also write this statement more simply as:

 C = A = B; 

A common use of this type of syntax is to evaluate some condition inside an if statement and simultaneously set a variable of type bool (the C# equivalent of Boolean in Visual Basic) to the result of this condition, so you can reuse this value later:

 // assume X and Y are some other variables that have been initialized bool B;  if ( B = (X==Y) )  DoSomething();  

This code looks daunting at first sight, but it is quite logical. Let's break it down. The first thing the computer will do is check the condition X==Y. Depending on whether X and Y contain the same data, this will either return true or false and this value will be assigned to the variable B. However, because the assignment operator also returns the value that has just been assigned to it, the complete expression B= (X==Y) will also return this same value (true or false). This return value will then be used by the if clause to determine whether to execute the conditional DoSomething() statement. The result of this code is that the condition X==Y is tested to determine whether the conditional statements should be executed, and at the same time you have stored the results of this test in the variable B.

The ternary operator

There is not space in this appendix to go over all the various operators that are available in C#. They are detailed in Chapter 2, "C# Basics," of this book. However, this section will mention the ternary operator(also known as the conditional operator) because it has a very unusual syntax. The ternary operator is formed from the two symbols ? and :. It takes three parameters and is actually equivalent to an IIf statement in Visual Basic. It is used syntactically like this:

 // B, X and Y are some variables or expressions. B is a Boolean. B ? X : Y 

The way it works is that the first expression (the one before the ? symbol) is evaluated. If it evaluates to true, then the result of the second expression is returned, but if it evaluates to false then the result of the third expression is returned instead. This provides an extremely compact syntax for conditionally setting the value of variable. For example, you can write

 string animal = (legs==8) ? "octopus" : "dog"; 

which yields the same result as:

 string animal; if (legs==8) animal="octopus"; else  animal="dog"; 

With the Visual Basic Iif function, this can be achieved with:

strAnimal = IIf(intLegs = 8, "octopus", "dog") 




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net