Different Data types in C#

Data Type in C#


In C# data type has been categorized in three types. these are below: -

  • Value Type
  • Reference Type
  • Pointer Types

Below is an overview of different data types exist in C# language

Value Type


In C# value type are those data types which stored into the stack. Stack is a memory type in our local system which allow the space for Value type variables. In value type variable data can assign directly. In C# value type have been defined under System.ValueType namespace.

Predefined Value Data type of C# are below



Let use discuss about all types one by one in details


Integer

An integer data type in C# has been categorized into three types. Integer type take 4 byte space for memory allocation for defined type.

    
int number1;
uint unsignedint;

Using this way we can define an integer value in our program. The default value of an integer value is 0. An integer type variable can hold value -2147483648 to 2147483647.

An unsigned integer value can store from 0 to 4294967295.


Long

An long data type in C# define as show in below. Long type takes 8 bytes space for memory allocation for defined type. Long have two types on is signed or simple long type and another is unsigned long type.


    long num1;
    ulong num2u;

An signed long type hold value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

An unsigned long type hold value from 0 to 18,446,744,073,709,551,615.

Default value of long type is 0L.


Float

An float data type in C# define as shown in below. Float types takes 4 byte space for memory allocation for defined types.

   float num;

A float type variable can hold value from -3.4 x 1038 to + 3.4 x 1038  till seven precision. Default value of float type is 0.0F


Double

An double data type in C# define as shown in below. Float types takes 8 byte space for memory allocation for defined types.

   double num;

A float type variable can hold value from 5.0 * 10 -324  to 1.7*10 308 till 15 digits of precision. Default value of double type is 0.0D.


Float

An float data type in C# define as shown in below. Float types takes 4 byte space for memory allocation for defined types.

   float num;

A float type variable can hold value from -3.4 x 1038 to + 3.4 x 1038  till seven precision. Default value of float type is 0.0F


Decimal

An decimal data type in C# define as shown in below. Decimal types takes 8 byte space for memory allocation for defined types.

   decimal num;

A float type variable can hold value from -7.9 x 1028 to 7.9 x 1028. Default value of decimal type is 0.0M.


Char

An char data type in C# define as shown in below. Char types takes 1 byte space for memory allocation for defined types. It store 16 bit unicode character.

   char val;

A char type variable can hold value from -128 to 127. Default value of char type is '\0'. '\0' denotes the sign of end of character. Char have also two types i.e. signed/ normal char type and another is unsigned char type.


Bool

An bool data type in C# define as shown in below. Bool types takes 1 bit space for memory allocation for defined types.

bool val;

A bool type variable can hold value either 0 or 1. 0 denotes false and 1 denotes true. Default value of bool type is 0.


Byte

An byte data type in C# define as shown in below. Byte types takes 1 byte space for memory allocation for defined types. It hold value 8 bit unsigned integer value which range start from 0 to 255. Its default value is 0.

Types of class in C#

 

What is Class in C#


Class is a user define data type, a class defines an object with the help of variables and methods. Class is a real time entity in terms of Programming language. In C# we use to define a class with Class keyword. A class can contains data types, properties, methods, events and their definitions. Here is an example of C# class and used terms. 

public class Program
{
    public string name = string.Empty;
    private int number;
    public int PropertyName{ get; set; }
    public Program()
         {
    }
    public void PrintName(string name)
    {
        Console.WriteLine("My name is {0}", name);
    }
}

  • In the above line of code "Program" is the name of the class followed by class code. There are two data type declare 1st one is name as string data type Which can contain only string type data. Another one is "number" which can contain only integer value. In the next step property has been defined of integer type named as "PropertyName".


  • A class can contain constructor also which name will be same as Class name as shown in the below line of code .

  public Program()  {

 

    }


  • Here Program() is constructor which name is as class name. The difference is that it defines as method. The main difference between and method is that a method can return a value but a constructor can't.

  • In the last we defined method inside the class. Here "PrintName(string name)" is a method.

Object

C# object of  class is a runtime entity. A class properties can access using the object of that class. To create an object class we use "new" keyword.


Through below statement, we can create an object of Program class.


Program     nameOfObject = new Program();


Here name of object can be any valid keyword such as program, prog or prm a valid keyword. 

Once an object created for any class it allocate memory of that class. An allocate memory of all existing variables declare or used into that class also.


In C# mainly it has four type of classes available



 

Partial Class

Partial classes are those class which can use in multiple files with the same name. Compile code of C# Partial class always generate single file. Main benefits of partial class, on same partial class multiple developers can work parallel with multiple files in the same partial class during big project. Partial class define by class name followed by Partial keyword. As shown in below lines of code.

    partial class Program
    {
        static void Main(string[] args)
        {

            Console.Write("This is 1st C# Program");
        }
    }
  

In the above line of code partial keyword used before class.

Abstract Class


An abstract class in C# does not contains full definition so in that case it must inherit into its child class. We can't directly create an object of an abstract class reason of not able to create an object of abstract class that an abstract class is incomplete class by it self so an incomplete line of code will give an error.

Remember :: An abstract class must inherit into it child class.

An abstract example shown in below line of code. Here Father is an abstract class. It contains two method FatherDetails() and Details(). FatherDetails() method does have implementation into the same class, so it required to define it with abstract class otherwise it will give an error.

    public abstract class Father {

        public abstract void FatherDetails();

        public void Details()
        {

        }
    }

    partial class Program : Father
    {
        Father father = new Father(); // Can't create an object of abstract class

        public override void FatherDetails()
        {

        }

        static void Main(string[] args)
        {

            Console.Write("This is 1st C# Program");
        }
    }

In the above line of code Father is abstract class and it is inherited into child class Program. The implementation of abstract method  FatherDetails() is mandatory into its child class i.e. Program (child class).

Father father = new Father();

line of  code will give an error as we know that we can not create the object of Abstract class.

Remember :: 

  • An abstract class must inherit into its child class.
  • Object can't create for an abstract class.
  • An abstract class defined with abstract keyword.
  • Abstract class always contains an abstract method which mandatory to implement into child class.

Static Class

An static class can not have instance. We can directly access using name of the class. It allows to create static member inside that class only. We can not inherit static class. We use static key word to create an static class as shown in the below example: -

    public static class Program
    {
        static string value;
        public static int Sum(int i, int j)
        {
            return (i + j);
        }
    }

In the above example Program is an static class. It contains a string type static variable "value" and one static method i.e. Sum(int i, int j) which can access out side the class using ClassName.MethodName() .

Some of the main feature of static class are: -

  • We can't create an instance of an static class.
  • An static class create using static keyword.
  • Only static member or member function may exist in a static class.
  • An static class can't be inherited.
  • In side static class only static constructor allowed.


Sealed Class 

No inheritance allowed from a sealed class. It use to restrict the inheritance in C#. Sealed access modifier use to declare a class as sealed. To access the members of sealed class it need to create the instance of that class. 

Here is an example of sealed class 

    public sealed class Program
    {
        int number;
        static string value;
        public int Sum(int i, int j)
        {
            return (i + j);
        }
    }

Some of the key points for a sealed class are

  • Access modifier not allowed over the sealed class.
  • Sealed class defined with sealed keyword.
  • Sealed class restrict inheritance in C#.
  • We must create the instance of sealed class to access their properties or methods.

Learn C# Program step by steps

 C# (C Sharp) 1st Program and Terms


Now, we will create a simple program to print a message and will under stand the terms used in C#. Before start i am declaring some of the points which actually necessary in terms of C# programming.

These are below :

    
  • Namespace
  • C# Class
  • Methods in Class
  • Class attributes
  • Main method
  • Statements and Expressions
  • Comments

We will discuss all of the pints one by one using this simple console application program:-





Here select choose 2nd option i.e. "Console App (.Net Framework)". Provide name of your application under "Name :" and select the location where you want to save your project/application and Click Ok button. Our console application will open as below.




Here is out 1st Program in which will discuss all the term.

Namespace

    In C# program on the top of the program we access namespace. Namespaces are the library which contains the definition of either predefined methods or attributes or keywords of C#. 
As we can see above we are accessing name space with "using" keyword followed by name of namespace and end with semicolon ";". In program we can have multiple namespace as per the requirement.

In this program we using System namespace which already defined in C# library.

At program label we can also define our namespace. Here "My1st_ConsoleApp" defined as namespace.

C# Class

Class is the main container of C# application. It contains the variable, methods. It is the real time entity.
In C# we declare class named as Program.



Method in Class

Here in the above program, there is a Main method which is entry point of C# console application. C# program from Main method always.

Statement & Expression

Inside the C# (e.g. Main) method write multiple lines of code which called Statement and write some of the logic e.g. to add, multiply two numbers are called expressions.

In this program, below line of code is statement which write a message on screen 

Console.Write("This is 1st C# Program"); 

Comments

In C# we define line of comments in two ways. Line of comments ignore by the C# compiler during compilation of Code.

  • Single line of Comments
  • Multiple line of comments

Single Line of Comments

We define single line of comments using double slash i.e. // line of comment here .

 Multiple Line Of Comments

 In C# multiple line of comments define using /* ----- Multiple Line of Comments ----- */. Multiple line of comments start with /* and end with */


In both cased Single line or multiple line of comments C# compiler ignore these line during program compilations.


The output of the above program is 

This is 1st C# Program.

In the below image all used term in the above program so user can have better understanding.




Tools for C# Programming

 

 Tools Required for C# (C Sharp) Programming

Programming in C#

 

 Programming in C# (C Sharp)


As we already discussed that we can create multiple types of application using .Net C#. Now we will discuss how we can create multiple type of application using .Net Framework.

  1. Console Application Project
  2. Window Application Project
  3. Web Application Project

Although we can create another types of project using .Net C# such as Class Library Project, Web Services, Window Services and so on.


  • Console Application Project
To create Console application using .Net Framework follow the below steps

  • Go to your window and search (type) Visual Studio. All installed versions of Visual Studio will show there select any one of them and Open. Visual Studio IDE which installed on your system as you can see in the below screen

C# Generations and Features

C# Generations and Features

C# Tutorial For Beginners

 Introduction of C#

C# is a simple & powerful object-oriented programming language developed by Microsoft. C# can be used to create various types of applications, such as web, windows, console applications, or other types of applications using Visual studio.

This tutorial will teach you basic C# programming and will also take you through various advanced concepts related to C# programming language.

C# was developed by Anders Hejlsberg and his team during the development of .Net Framework. C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.

 



Below are the point which causes for widely use of C# 

  • It is a modern & latest programming language
  • It supports object oriented programming.
  • It is component oriented.
  • It is easy to learn.
  • It is a structured language.
  • It produces efficient programs.
  • It can be compiled on a variety of computer platforms.
  • It is a part of .Net Framework.
  • Strong Programming Features of C#

Although C# constructs closely follow to C and C++ as base language and being an object-oriented programming language. It has numerous strong programming features that make it endearing to a number of programmers worldwide.