C# Primitives


This entry is part 1 of 4 in the series C# Primitives and Expressions

This post serves as an introduction to primitives and expressions. A primitive is a simple type. The post C# Introduction lists all of the C# series of posts we have at this site.

A primitive is a data type. A primitive data type is the most basic type provided by a programming language. In most programming languages, all basic data types are built-in. Basic types include integers, floating point numbers and boolean. Strings in C# are not primitive types.

What are the built-in types in C#? They are: bool, byte, sbyte, char, decimal, double, float, int, uint, long, ulong, object, short, ushort and string. All of the types listed, except object and string, are referred to as simple types. So, string, array, enum, and class are not primitive types.

The C# type keywords and their aliases are interchangeable. For example, you can declare an integer variable by using either of the following declarations. You can create a variable or a constant. Start with a type, an identifier and a value. You cannot use it without assigning a value. We cannot define a constant without setting its value. Identifiers cannot start with a number. Identifiers cannot have whitespace. Identifiers cannot be reserve keywords. Use meaningful names.

int y;  // not initialized
int x = 123;  // optionally assign a value
System.Int32 x = 123;
const float pi = 3.1415;
float numb = 2.876f  // the f says float (double is default)

There are three types of casing you can use.

  • camelCase
  • PascalCase
  • strHungarianNotation (generally not used in C# by convention)

Variable Naming

  • Must start with a letter, underscore or @
  • Subsequent characters may be letters, underscore or numbers
  • Cannot contain spaces or symbols (except underscore)

Literals

  • Are numbers or strings
  • Some numbers have suffixes (u, U, l, L, f, F, m, M)
  • Escape sequences for string literals
  • Strings are reference types

Verbatim Strings

You may specify strings verbatim with the @ symbol:

@”C:\temp\myfile.txt”

We have posts on complex types. The introduction to complex types is at the post called C# Complex Types Introduction.

To display the actual type for any C# type, use the system method GetType(). For example, the following statement displays the system alias that represents the type of myVariable:
/p>

Console.WriteLine(myVariable.GetType());

Overflowing

In C# we don’t have overflow checking. If we store 255 into a byte, and add one to it we get zero. If at run time you would rather throw an exception you need to write your code within a block prefixed with the checked keyword. The program will crash unless you handle the exception. In the real world, checked is seldom used by developers as they would probably use a larger type. Incidentally, you can delete a line of code by placing the cursor on the line and pressing Crtl+X.

checked
   {
      byte number = 255;
      number = number + 1;
   ]

Here is some example code. In the Console.WriteLine() we are using a format string.

        static void Main(string[] args)
        {   // keywords are blue
            int count = 10;
            float myFloat = 2.09f;    // f tells compiler float 
            char myCharacter = 'a';
            string myString = "John";  // not a primitive
            bool IsWorking = true;   // or false
            Console.WriteLine("{0} {1}", byte.MinValue, byte.MaxValue);
            Console.ReadKey();
        }
Series NavigationC# Expressions >>