C# Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.2

Data Literals

This section describes 5 types of data literals used in C#: Boolean, Integer, Real, Character and String.

The main goal of a computer programming language is to help human to write instructions to process data. So the first thing to learn about a computer language is to know how data is represented in this language.

In C#, data is divided into 5 different types:

  • Boolean
  • Integer
  • Real
  • Character
  • String

When a value of any data type is entered directly into a C# program, it is called a literal. The following rules defines some basic formats of how literals of different data types can be entered into a C# program. We will introduce more special formats later.

Rule: There are only two boolean literals: true, false.

Rule: Integer literals can be entered in a decimal format. For example: 0, 555, -777, 911.

Rule: Real literals can also be entered in a decimal format. For example: 0.0, 555.0, -777.0, 3.14159.

Rule: Character literals can be entered within two single quote characters. For example: 'a', 'A', '0', '9'. This format is fine to enter regular characters. But there are also some special printing commands that are treated as special characters, like backspace, tab, return and form feed.

Rule: Character literals for special characters can be entered as an escape sequence within two single quote characters. For example: '\t', '\r', '\n', '\'', '\"', '\\'.

Rule: String literals can be entered within two double quote characters. Escape sequence is allowed to represent special characters. For example: "911", "Hello world!", "Joe said \"Hello\" to me".

In order to experiment what we have learned in this section, we need to learn one more thing about the WriteLine method we used in our first program. The WriteLine method can take more than one parameter using the following format:

WriteLine("...{0}...{1}...{2}...", literal_1, literal_2, literal_3);

In this format, the first parameter is the formatting string which contains place holders like {0}, {1} and {2}. The second parameter and any additional parameters can be any types of literals. Each place holder has an index number, and it will be replaced by the corresponding literal while the method is executed. During the replacement process, each literal will be converted into a string first. Then it will be placed into the formatting string to occupy the place holder's place. Note that the index number of place holder starts with 0, not 1.

Last update: 2010.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

Data Type and Variables

Data Literals

 Data Literals - Example

 Variables and Assignment Statements

 Variables and Assignments - Example

 Arithmetic Operations

 Arithmetic Operations - Example

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 Memory Usages of Processes

 Multithreading in C#

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2012
Data Literals