Freelance Writing Jobs | Today's Articles | Sign In

 
Browse Sections

Value Types


Before we go and understand the command structure of C#, I believe that it is important to first understand the data types that are available. Data types are the fundamental blocks of any programming language, some have plenty; others have scarce. Real-world information, such as area and perimeter of a shape, can be represented through a programming language's data type.

Like any programming language, C# has some basic data types that comes in-built with the language. You may ask, what makes C# any different from other languages? There is nothing new to C# that other languages cannot have. However, it is in the implementation of these data types that makes a lot of difference. In .Net, all data types are now the same and that is irrespective of any programming language.

The Fundamental Problem
The problem we now face is that different languages have their own implementation of data types. Therefore, it is never guaranteed that an integer in Visual Basic will have the same range values for an integer in C++. This difference makes software interoperability very difficult. The .Net platform eliminates this discrepancy by having a standard representation and range limit for an integer. An integer in .Net is the same for all programming languages. C# takes the natural progression and adopts these data types as part of the language.

You can now start to get an appreciation of the problems C# and .Net are trying to address. In C#, types are grouped into two: -

  • Value types
  • Reference types

What are value types?
Value types allow you to store values in a variable with a certain type. It is a reserved space in memory and your program directly manipulates data stored in that space. Value types are further grouped into five. Here's a list of value types available in C#: -

  • Integral type: sbyte, byte, short, ushort, int, uint, long, ulong and char
  • Floating point types: float and double
  • Decimal type: decimal
  • Bool type: bool
  • Enumeration type: enum

If you're a C/C++ programmer, you'll immediately notice that there are two new types; decimal and bool. You can use the decimal type to represent monetary or financial information. It has greater precision than float or double, however the decimal type can hold smaller range.

The bool data type, which you've probably worked it out to mean Boolean, can only have two values True of False. C/C++ programmers are familiar that any non-zero value is a True condition, which is a cause of plenty of errors and bugs. Some of you will argue that bool already exist in C++ and that it is not new. In C#, bool is used extensively in decision commands. The "non-zero" representation for a True condition no longer exists in C#.

The copyright of the article Value Types in C# Programming is owned by Jose Aniceto. Permission to republish Value Types in print or online must be granted by the author in writing.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic