- SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# Data types

C# Data types

shape Description

Data types defines the type of variables used. C# is a strongly typed language and hence required to tell the compiler about which data types are required to use every time when using a variable. Below mentioned are most used data types and how they work.

Value Types

shape Description

Most commonly used Datatypes are discussed below:
  • bool is one of the simplest data types. It accepts only 2 values – true or false.
  • int is short for integer and accepts only numbers. When working with numbers, int is the most commonly used data type. Integers have several data types within C#, depending on the size of the number.
  • string is used for storing text(charecters). In C#, strings are immutable, which means that strings are never changed after they have been created. When using methods which changes a string, the actual string is not changed - a new string is returned instead.
  • char is used for storing a single character.
  • float is one of the data types used to store numbers and decimals.

shape Example

Simple example to get the size of int type: [csharp] using System; using System.Text; namespace SPLessons { class Program { static void Main(string[] args) { Console.WriteLine("Size of Int:",sizeof(Int32)); //Print Size of int Console.ReadLine(); } } } [/csharp] Output:

Reference Types

shape Description

The reference types do not contain the actual data. They only point to reference of the variable. They refer to a memory location. Using multiple variables, the reference types can refer to a memory location. Example of built-in reference types are
  1. Object Type
  2. Dynamic Type
  3. String Type
DataTypes Size Values
String Variable Length 0-2 billion characters
Object - -

Object Types

shape Description

Object is a blueprint for System.Object class. It is the base class of all the data types in Common Type System. It can be assigned with value types, reference types, predefined or user-defined types. Type Converstion:
  • When a value type is converted to an object type, it is called Boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.
 

shape Example

Boxing Example: Object obj; obj=100 // this is Boxing

Dynamic Type

shape Description

Dynamic data type variable can store any type of value in it. C# is a strongly typed language and it supports Type checking which is performed at run-time for Dynamic type variables.

shape Syntax

Dynamic = value; For example, dynamic d=20; //Assingnement of value to dynamic variable

String Type

shape Description

The string type is an alias for the System. String class allows only string to store. It is derived from object type. For example:
  • String str=”SP Lessons” // quoted
  • @”SP Lessons”           //@ quoted

Pointer Type

shape Description

Pointer type variables are used to store the memory address of another type. C# have the same capabilities as the pointers in C or C++.

shape Syntax

Type * identifier For example, Char * c; int* ptr;