serving the solutions day and night


Saturday, September 18, 2010

C# - Program Structure, Data Types, Type Casting (Conversion)

Program Structure
//the using keyword is used to include the System namespace in the program.
using System;
//the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.
namespace HelloWorldApplication
   //a class declaration, the class HelloWorld contains the data and methods
   class HelloWorld
      // the mehod declaration
      void Print()
C# is case sensitive.
All statements and expression must end with a semicolon (;).

Data Types
Value types 
- value type variables can be assigned a value directly.
- bool, byte, char, decimal, double, float, int, long, sbyte, short, uint, ulong, ushort
- get the exact size of a type or a variable - sizeof(int)
Reference types
- The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. in other words, they refer to a memory location.
- Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
- object, dynamic, and String.
- object obj = 100; //it will assign ar comiple time
- dynamic d = 100; //it will assign at run time
- String s ="C# code"; or @"c# code";
Pointer types (Unsafe Codes)
- store the memory address of another type.
- char* cptr;

Type Conversion (Type Casting) -  converting one type of data to another type, 2 forms
  - Implicit type conversion
  - performed by in a type-safe
  - int i = 123456789;
   long l = il
  - Derived d = new Derived();
   Base b = d;
  - Explicit type conversion
  - conversions are done explicitly  by users using the pre-defined functions. Explicit conversions require a cast operator.
  - double d = 1234.67;
   int i= (int)d; // 1234
  - type conversion methods -   ToBolean, ToByte, ToChar, ToString()
  - int i = 75; i.ToString();  

No comments: