C Sharp/Literals

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables

C# - Operators
C# - Flow Control
C# - Variables II
C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

[edit] C# Literals

A literal identifier specifies the type a value should be. This is done by placing a letter or sequence of characters at the end of the literal. Take a look at the following table:


C# Literals

Types Suffix Use
uint u,U 5U
ulong ul,UL 15U
long l,L 1000L
float f,F 0.5F
double d, D 1.22D
decimal m,M 0.9M


In C# there are many decimal point literals (float, decimal, double) so how will the compiler know which value to choose. For some reason when you don't specify what kind of decimal point number the variable is the compiler automatically chooses a double. For example:

float a = 0.9      //This is not acceptable and will produce errors

The compiler thinks that 0.9 is a type double and cannot convert it to a float. To make the compiler convert 0.9 to a float put a 'f' and the end of 0.9. All declaration must have a literal except integer and double values. Take a look at the code below.

  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. float a = 0.9f;
  8.  
  9. Console.WriteLine(a);
  10. Console.Read();
  11. }
  12. }

This code will compile fine with no errors. The chart above notes all the types that require a character literal.