News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Jan 3 2015 10:38PM     Mohammad Shaik
  0 Comments    1867 Views  
Here Mohammad Shaik provided a short tutorial with example, about DataTypes and Type Conversions in C# programming.
DataTypes
What is DataType ?
  1. A data type in a programming language is a set of data with values having predefined characteristics.
  2. C# was developed by Anders Hejlsberg and his team during the development of .Net Framework.
  3. Examples of data types are: integer, floating point unit number, character, string, and pointer.
  4. Usually, a limited number of such data types come built into a language. The language usually specifies the range of values for a given data type, how the values are processed by the computer, and how they are stored.
In C#, variables are categorized into the following types:
  • Value types
  • Reference types
  • Pointer types
Value types
  1. Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value.
  2. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.
  3. All value types are derived implicitly from the System.ValueType.
The following table lists the available value types in C#:
TypeRepresentsRangeDefault
Value
boolBoolean valueTrue or FalseFalse
byte8-bit unsigned integer0 to 2550
char16-bit Unicode characterU +0000 to U +ffff'\0'
decimal128-bit precise decimal values with 28-29 significant digits(-7.9 x 1028 to 7.9 x 1028) / 100 to 28 0.0M
double64-bit double-precision floating point type(+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
float32-bit single-precision floating point type-3.4 x 1038 to + 3.4 x 10380.0F
int32-bit signed integer type-2,147,483,648 to 2,147,483,6470
long64-bit signed integer type-923,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
sbyte8-bit signed integer type-128 to 1270
short16-bit signed integer type-32,768 to 32,7670
uint32-bit unsigned integer type0 to 4,294,967,2950
ulong64-bit unsigned integer type0 to 18,446,744,073,709,551,6150
ushort16-bit unsigned integer type0 to 65,5350
Example:

using System;

class Program
{
    static void Main()
    {
        int value = 10;
        value += 5;
        Console.WriteLine("The Value is :" + value);
    }
}

When the above code is compiled and executed, it produces the following result:
The Value is: 15
Reference Types
  1. The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.
  2. In other words, they refer to a memory location. Using more than one variable, 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. Example of built-in reference types are: object, dynamic and string.
Boxing & UnBoxing
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.
Example:

using System;
 
namespace Reference_Parameter
{
  class Program
   {
     public static void qube(ref int num)
      {
        num = num * num * num;
      }
     static void Main(string[] args)
      {
        int original = 9;
        Console.Write("\ncurrent value of Original is {0}\t", original); 
        Program.qube(ref original);
        Console.WriteLine("\nNow the current value of Original is {0}\t", original);
        Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result:
current value of Original is 9
Now the current value of Original is 729
String Type
The String Type allows you to assign any string values to a variable. The string type is an alias for the System.String class. It is derived from object type.
For Example:

string str = "Developer Tips";

Object Type
The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. So object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.
For Example:

object obj;
obj = 100; // this is boxing

Pointer Types
Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as in C or C++.
Syntax:

type* identifier;

Example:

char* cptr;
int* iptr;

Type Conversion
Type conversion is basically type casting, or converting one type of data to another type. In C#, type casting has two forms:
Implicit type conversion - These conversions are performed by C# in a type-safe manner. Examples are conversions from smaller to larger integral types and conversions from derived classes to base classes.
Explicit type conversion - These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
C# Type Conversion Methods
C# provides the following built-in type conversion methods:
S.NoMethods & Description
1ToBoolean
Converts a type to a Boolean value, where possible.
2ToByte
Converts a type to a byte.
3ToChar
Converts a type to a single Unicode character, where possible.
4ToDateTime
Converts a type (integer or string type) to date-time structures.
5ToDecimal
Converts a floating point or integer type to a decimal type.
6ToDouble
Converts a type to a double type.
7ToInt16
Converts a type to a 16-bit integer.
8ToInt32
Converts a type to a 32-bit integer.
9ToInt64
Converts a type to a 64-bit integer.
10ToSbyte
Converts a type to a signed byte type.
11ToSingle
Converts a type to a small floating point number.
12ToString
Converts a type to a string.
13ToType
Converts a type to a specified type.
14ToUInt16
Converts a type to an unsigned int type.
15ToUInt32
Converts a type to an unsigned long type.
16ToUInt64
Converts a type to an unsigned big integer.
Example:

namespace TypeConversionApplication
{
    class StringConversion
    {
        static void Main(string[] args)
        {
            int i = 75;
            float f = 53.005f;
            double d = 2345.7652;
            bool b = true;

            Console.WriteLine(i.ToString());
            Console.WriteLine(f.ToString());
            Console.WriteLine(d.ToString());
            Console.WriteLine(b.ToString());
            Console.ReadKey();
            
        }
    }
}

When the above code is compiled and executed, it produces the following result:
75
53.005
2345.7652
True
I hope this page will helps to learn about DataTypes and Type convertion in C#. Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: