February 26, 2021
Hot Topics:

The C# Data Types and Programming Constructs

  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

In this article, we will examine the fundamentals of the C# language, which include the following:

  • Data Types and Variables
  • Programming Constructs

Data Types and Variables

Data Types are an integral part in any programming language. They represent how to express numbers, characters, strings, decimal point values, and so forth. To illustrate, if you want to express 25 in C#, you can write a code as shown below:

Listing 1

int x = 25

In the above code, int represents the integer data type. x denotes a variable name. Variables are nothing but a storage area in the memory. 25 is the value for the variable x. Without a variable you cannot store values. Keep in mind that int is an alias for the data type Integer. It has a fixed limit beyond which the data type will not hold values. Like Integer, all data types have aliases and a fixed range of values.

You should use a data type best suited to your programming needs. For example, if you are writing code for developing a calculator, it's better to use data types, which will accept large values. This will avoid runtime program errors.

Let's look at another scenario. The data type Byte accepts values only up to 255. If you try to apply a value of 300 for the corresponding variable, there will be errors during program execution. These errors are called Exceptions; they will be discussed in another article. If you had applied the data type Integer for the above program, this problem would have been avoided. Please go through Table 1 for a detailed list of all data types.

Table 1     List of Data Types
.NET Class Related DataType .NET Class Related DataType
System.Byte byte System.Sbyte sbyte
System.Int16 short System.Int32 int
System.Int64 long System.UInt16 ushort
System.UInt32 uint System.UInt64 ulong
System.Single float System.Double double
System.Object object System.Char char
System.String string System.Decimal decimal
System.Boolean boolean    

Basically, data types in C# are classified into two types: value-based and reference-based. Let's examine each of these types in detail.

Value-Based Data Types include all numerical data types such as Integer, Float, Byte, and so forth. Here you are working directly with values. When you try to assign one value type to another, a bit wise copy is achieved as shown in Listing 2:

Listing 2

using System;
struct Valdata
  public int a;
  public int b;

class Valtype
  public static void Main(string[] args)
    Valdata v = new Valdata();
    v.a = 200;
    v.b = 300;
    Valdata v1 = v;

    v1.a = 800;

Note that even if you change the values of one variable, the value of another variable won't change.

Reference types include Classes and Interfaces. Here, if you change the value of one variable, the value of the other variable also reflects the same value. Also, after creating an object of a class, you are working directly on that object and not with the actual values as in value types. Hence, copies of reference types results in a shallow copy. To illustrate, change the definition of the above type from a C# structure to a C# class (in the above code) and observe the result.

Page 1 of 2

This article was originally published on September 19, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date