Braintique.com header
Left Navigation Bar

C# Types

Everything in C# is a type—so we had better get C# and types under our belt right away.

So far in this article I’ve used a few types—int, and string—to provide examples of constants, variables, and enumerations. The table below shows the complete list of C# predefined, or intrinsic, types (also sometimes called primitive types). These predefined types are declared using a keyword that functions as an alias or shorthand for the type provided by the system. For example, the keyword int refers to the System.Int32 type and the keyword string refers to the System.String type. (Stylistically, you should use the keyword rather than the fully-qualified name of the underlying type to refer to the type.)

Predefined C# Types

Keyword

.NET Type

Bytes Reserved in Memory

Description

byte

System.Byte

1

Unsigned 8-bit integer value (0 to 255).

char

System.Char

1

Unicode character.

bool

System.Boolean

1

Boolean type: either true or false. Visual Basic users should note that the bool type can contain only true or false, and not an integer value of 0, 1, or –1 as in some versions of VB.

sbyte

System.Sbyte

1

Signed 8-bit integer value (–128 to 127).

short

System.Int16

2

Signed 16-bit integer value (–32,768 to 32,767).

ushort

System.Uint16

2

Unsigned 16-bit integer value (0 to 65,535).

int

System.Int32

4

Signed 32-bit integer value (–2,147,483,647 to 2,147,483,647).

uint

System.Uint32

4

Unsigned 32-bit integer value (0 to 4,294,967,295_.

float

System.Single

4

Single-precision floating point number.

double

System.Double

8

Double-precision floating point number.

decimal

System.Decimal

8

Fixed-precision number up to 28 digits and the position of the decimal place. Used in financial calculations. Requires an m or M appended (see example in the Constants section earlier in this article).

long

System.Int64

8

Signed 64-bit integer.

ulong

System.Uint64

8

Unsigned 64-bit integer.

object

System.Object

N/A

All data types, predefined and user-defined, inherit from the System.Object class, aliased as object. The object type is a reference type rather than a value type.

string

System.String

N/A

The string reference type represents a sequence of Unicode characters. See Part III of this course.

In the table, object and string are reference types, meaning that a pointer to the data is stored rather than the data itself, while the other types in the table are value types (meaning that the actual value of the type is stored in memory).

In addition to the predefined types, you can use C# to create custom, reference types. In fact, the heart of programming in C# is creating your own types by designing classes (which are reference types) that serve as a blueprint for objects. Besides classes, other important reference types include arrays (see Part II of this course), delegates, and interface types.

You’ve probably already heard the phrase “C# is a strongly typed language”—and may, indeed, already know what it means. However, this whole area of strong typing and type conversions is one of the most frustrating things for programmers coming to C# from a weakly typed environment such as Visual Basic—so this section is especially for those of you in that boat.

In a strongly typed language, all variables have a type that must be declared. In addition, the compiler verifies the type consistency of expressions (and expressions are always of a type defined by the C# language, or are user-defined types).

To make VB .NET strongly typed in much the way that C# .NET is, you’d run VB .NET with the compiler option Strict set to On, accomplished either in the IDE Options dialog, or by adding the statement Option Strict On at the beginning of each code module. Note that VB6 and earlier versions of Visual Basic had no way to enforce strong typing—and were by definition weak type environments.

As a practical matter, working in a strongly typed language means that you need to need to be very clear about the type of the information that will be stored in a variable. Strong typing enforces programming discipline and clarity about the contents of variables. It also prevents possible program errors that can occur in weakly typed environments when the compiler finds the wrong kind of value in a type. Another way of thinking of this is that weak typing allows a programmer to be lazy—in a possibly dubious type conversion, the compiler “guesses” what the programmer most likely meant (which can occasionally introduce errors).

The trade-off for the benefits of strong typing is more work up front. For one thing, you must explicitly declare the type of all variables (which is good programming practice even in weakly typed environments, where it may not be required). For another, you must pay close attention in your code every time a value of one type is converted to another. Much of the time, you must provide explicit guidance to the compiler using casting or a conversion method about the type conversion you’d like.

Type conversion can get pretty convoluted, and can involve multiple conversions within a single statement. For example, the statement

mbb = (MessageBoxButtons) Convert.ToInt16(rb.Tag.ToString());

involves three conversions that have to be specified by the programmer:

  • A cast to type MessageBoxButtons, indicated by the type in parentheses
  • A conversion to type short using the ToInt16 method of the System.Convert object
  • A conversion to type string using the ToString method inherited by all objects from System.Object

A simple example is probably the best way for getting a feeling for the difference between working in a weakly and strongly typed environment.

If you run the following code in VB .NET (with Option Strict disengaged)

Dim theFloat As Double = 3.5
Dim X As Integer = 2
X = X + theFloat
MessageBox.Show(X)

the program will run without syntax errors. The value of theFloat will be rounded up and off to 4 when it is added and assigned to the integer X.

Next, in the message box statement, the integer argument X is automatically converted to a string type, and the value 6 is displayed.

This is convenient if it is what you want to have happen, but it is also the possible source of the introduction of errors in more complex programs if you are not counting on the round-up. Adding 3.5 and 2 and getting 6 as the integer result is not unreasonable. However, adding 2.5, 3.5, and 2 and getting 9—which is what would happen in VB—is pretty weird (8 would be a better result).

The comparable code in C#,

double theFloat = 3.5;
int X = 2;
X = X + theFloat;
MessageBox.Show(X);

simply will not compile due to several conversion-related syntax errors.

You can correct the C# code snippet by casting the double-type variable theFloat to int and using the ToString method to display the contents of the variable X in the message box:

double theFloat = 3.5;
int X = 2;
X = X + (int) theFloat;
MessageBox.Show(X.ToString());

his code compiles and runs just fine without any syntax errors, but—interestingly—it produces different results than the VB code, truncating theFloat to 3 and displaying 5 as the result. The (int) cast has simply taken the whole-number portion of theFloat variable. To perform the round-off operation that you’d normally expect when converting 3.5 to an integer value (e.g., 4), you need to use the explicit Convert.ToInt16 method:

double theFloat = 3.5;
int X = 2;
X = X + Convert.ToInt16(theFloat);
MessageBox.Show(X.ToString());

So the moral here is: know your types, and be sure to understand the implications of the conversions you specify.

Let’s move on to the next topic: the second part of this course shows you how to work with arrays in C#.

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write