find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

Data Type C# Example

 

 

C# - Data Types

In C# language, Data Types are used to define a type of data and variable  are used to build values which are used within an application.C# is a strongly typed programming language because  each type of data (such as character,float, integer and so forth) is predefined as part of the programming language.
 

Syntax of Defining C# Data Types

 

<Data Type> <Variable Name>;
<Data Type> <Variable Name> = <Value>;

 

<Data Type> - It’s a type of data the variable can hold such as string, decimal,integer,  etc.
<Variable Name> - It’s a name of the variable to hold the values in our application.
<Value> - Assigning a required value to the variable.

 

Example: Variables of Different Data Types

string stringVal = "Hello World!!";
int intVal = 100;
float floatVal = 10.2f;
char charVal = 'A';
bool boolVal = true;

Data types is mainly divided into three categories in C#

⦁    Value Data Types
⦁    Reference Data Types
⦁    Pointer Data Type

 

1.Value Data Types : 

The Value Data Types will directly store the variable value in memory and also it will also accept both signed and unsigned literals.Value Data types  are derived from the class System.ValueType.
Different Value Data Types are given below in C# programming language 

i.Signed & Unsigned Integral Types


        There are 8 types of integral  which provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or unsigned form.

Name Type name Type Size(BITEs) Range Default Value
sbyte System.Sbyte signed integer 8 -128 to 127 8
short System.Int16 signed integer 16 -32768 to 32767 0
Int System.Int32 signed integer 32 -231 to 231-1 0
long System.Int64 signed integer 64 -263 to 263-1 OL
byte System.byte unsigned integer 8 0 to 255 0
ushort System.UInt16 unsigned integer 16 0 to 65535 0
uint System.UInt32 unsigned integer 32 0 to 232 0
ulong System.UInt64 unsigned integer 64 0 to 263 0



ii.Floating Point Types :

    There are 2 types of floating point data types which contain the decimal point.

⦁    Float

  It has 7 digit Precision.It is 32-bit single-precision floating point type.
                           Like :- flot a=3.5f;

⦁    Double: It has 14 – 15 digit Precision and 64-bit double-precision floating point type.
 

Name Type Name Size(BITS) Rang Default Value
float
System.Single
32 3.4 × 1038 to +3.4 × 1038 0.0F
double
System.Double
64 ±5.0 × 10-324 to ±1.7 × 10308 0.0D


iii.Decimal Types:
 

 The decimal type is a 128-bit data type suitable for monetary and financial calculations. It has 28-29 digit.for example 
decimal a=12.3m;


To make a table 
decimal    System.Decimal    128    -7.9 × 1028 to 7.9 × 1028    0.0M
 

iv.Character Types :

The character types represents the 16-bit Unicode character or UTF-16 code unit .
To make a table here


char    System.Char    16    U +0000 to U +ffff    ‘\0’
 


// C# program to demonstrate  
// the above data types 

using System;
namespace FindAndSolve
{

   public class DemoExample
    {

        // Main function 
       public static void Main()
        {

            // declaring character here 
         
  char c = 'F';

            // Integer data type is generally 
            // used for numeric values 

            int i = 10;

            short sh = 20;

            // this will give error as number 
            // is larger than short range 

            // long uses Integer values which  
            // may signed or unsigned 

            long lo = 1000;

            // UInt data type is generally 
            // used for unsigned integer values 

            uint ui = 75;

            ushort ust = 66;
            // this will give error as number is 
            // larger than short range 

            // ulong data type is generally 
            // used for unsigned integer values 

            ulong ulo = 3624562;

            // by default fraction value 
            // is double in C# 

            double dou = 9.358674534;

            // for float use 'f' as suffix 
            float fl = 5.7330632f;

            // for float use 'm' as suffix 
            decimal deci = 292.5m;

            Console.WriteLine("char: " +c);
            Console.WriteLine("integer: " + i);
            Console.WriteLine("short: " + sh);
            Console.WriteLine("long: " + lo);
            Console.WriteLine("float: " + fl);
            Console.WriteLine("double: " + dou);
            Console.WriteLine("decimal: " + deci);
            Console.WriteLine("Unsinged integer: " + ui);
            Console.WriteLine("Unsinged short: " +ust);
            Console.WriteLine("Unsinged long: " + ulo);
        }
    }
}

 

Output:

char: F
integer: 10
short: 20
long: 1000
float: 5.733063
double: 9.358674534
decimal: 292.5
Unsinged integer: 75
Unsinged short: 66
Unsinged long: 3624562

 

 

// C# program to demonstrate the Sbyte 
// signed integral data type 

using System;
namespace FindAndSolve
{

   public class DemoExample
    {

        // Main function 
       public static void Main()
        {
            sbyte s = 126;

            // sbyte is 8 bit  
            // singned value 

            Console.WriteLine(s);

            s++;
            Console.WriteLine(s);

            // It overflows here because 
            // byte can hold values  
            // from -128 to 127 

            s++;
            Console.WriteLine(s);

            // Looping back within  
            // the range 

            s++;
            Console.WriteLine(s);
        }
    }
}

 

OUTPUT:
126
127
-128
-127
 

// C# program to demonstrate  
// the byte data type 

using System;
namespace FindAndSolve
{

   public class DemoExample
    {

        // Main function 
       public static void Main()
        {
            byte s = 0;

            // byte is 8 bit  
            // unsigned value 

            Console.WriteLine(s);

            s++;
            Console.WriteLine(s);

            s = 254;

            // It overflows here because 
            // byte can hold values from 

            // 0 to 255 
            s++;
            Console.WriteLine(s);

            // Looping back within the range 
            s++;
            Console.WriteLine(s);
        }
    }
}
 

OUTPUT:

0
1
255
0
 

V.Boolean Types:

 It has to be assigned either true or false value.
bool    System.Boolean    True / False

For Example.

 

// C# program to demonstrate  
// the byte data type 

using System;
namespace FindAndSolve
{

   public class DemoExample
    {

        // Main function 
       public static void Main()
        {
            byte s = 0;

            // byte is 8 bit  
            // unsigned value 

            Console.WriteLine(s);

            s++;
            Console.WriteLine(s);

            s = 254;

            // It overflows here because 
            // byte can hold values from 
            // 0 to 255 

            s++;
            Console.WriteLine(s);

            // Looping back within the range 
            s++;
            Console.WriteLine(s);
        }
    }
}

OUTPUT:

0
1
255
0
v.Boolean Types:
 It has to be assigned either true or false value.
bool    System.Boolean    True / False


For Example.

 

// C# program to demonstrate the 
// boolean data type 

using System;
namespace FindAndSolve
{

   public class DemoExample
    {

        // Main function 
       public static void Main()
        {
            // boolean data type 
            bool a = true;
            if (a == true)
                Console.WriteLine("Hello Rahul");
        }
    }
}
 

OUTPUT:
Hello Rahul

 

2.Reference Data Types:

Reference Data Types will contain a memory address of variable value because the reference types  was not store the variable value directly in memory.

String : It represents a sequence of Unicode characters and its type name is System.String. So, string and String are equivalent.
 

Data Type .Net Type Size Range
string String
Variable Length
0 to 2 billion Unicode characters
object Object - -


For example

 

// C# program to demonstrate  
// the Reference data types 

using System;
namespace FindAndSolve
{

   public class DemoExample
    {

        // Main Function 
       public static void Main()
        {

            // declaring string 
            string f = "find";

            //append in a 
            f += "and";
            f = f + "solve";
            Console.WriteLine(f);

            // declare object obj 
            object newObj;
            newObj = 20;
            Console.WriteLine(newObj);

            // to show type of object 
            // using GetType() 

            Console.WriteLine(newObj.GetType());
        }
    }
}

 


OUTPUT:
findandsolve
20
System.Int32
 

3.Pointer Data Types:

Pointer Data Types will contain a memory address of variable value.There are tow symbols ampersand (&) and asterisk (*) in c# language.

ampersand (&): This is Address Operator and it is used to determine the address of a variable.
asterisk (*):  Indirection Operator and it is used to access the value of an address.

For Example

type* demo;
int* a;
int* b;
// Note: This program will not work on 
// online compiler 
// Error: Unsafe code requires the `unsafe'  
// command line option to be specified 
// For its solution: 
// Go to your project properties page and 
// check under Build the checkbox Allow 
// unsafe code. 

using System;
namespace FindAndSolve
{
    class DemoExample
    {
        // Main function 
        static void Main()
        {
            unsafe
            {
                // declare variable 
                int f = 10;
                // store variable n address  
                // location in pointer variable p 

                int* a = &f;
                Console.WriteLine("Value :{0}", f);
                Console.WriteLine("Address :{0}", (int)a);
            }
        }
    }
}


 

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response