Slide 8.6: Data types
Slide 8.8: Variables
Home

Data Types (Cont.)


Object Types
The object type (System.Object class) is the ultimate base class for all data types. The object types can be assigned values of any other types. However, before assigning values, it needs type conversion. When a value type is converted to 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:
   object obj;     obj = 100;     // this is boxing
Dynamic Types
You can store any type of value in the dynamic data type variable. Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time. Syntax for declaring a dynamic type is dynamic <variable_name> = value;; e.g.,
     dynamic d = 20;
String Types
The string type (System.String class) allows you to assign any string values to a variable. The value for a string type can be assigned using string literals in two forms: quoted and @quoted. For example,
     String str = "Tutorials Point";
A @quoted string literal looks as follows −
     @"Tutorials Point";
Pointer Types
Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as the pointers in C or C++. Syntax for declaring a pointer type is type* identifier;. For example,
     char* cptr;     int* iptr;
An Example
A string is enclosed by double quotes. The example shows string concatenation by using the operator ‘+’. The IF statement decides which greeting is to be printed based on the checked radio button. Two radio buttons, four labels (one for the output), one textbox, and one button are used for this example.

 using System;
 using System.Windows.Forms;

 namespace WindowsFormsApp4 {
   public partial class Form1 : Form {
     public Form1( ) { InitializeComponent( ); }

     private void Button_Click( object sender, EventArgs e ) {
       string greeting;
       if ( radioButton1.Checked )
         greeting = "Good morning, ";
       else
         greeting = "Good afternoon, ";
       result.Text = greeting + user.Text + "!";
     }
   }
 }