Skip to main content

Data-type Concept in C#

Datatype Concept in C#:-

It is used to provide a pattern of data and the size of data in memory. The data type will be used into variable, constant, and method declaration.


Data type under variable:-

Datatype variablename=value         int x=10

Data type under constant:-

const Datatype constantname=value    const int y=100

C# has two different types of Datatype

1)  Primitive:- 

 It Supports all datatype of C and CPP languages.

int     --------->  4byte   (C# has Int16, Int32, Int64)

float  ----------> 4byte, 8 bytes ( float size depends on ox base 32 and 64) 

char  -----------> 2byte

string  --------->  Based on no of chars

boolean  ------>  It returns a true or false value using 1 and 0


2)  Derived Datatype:-
   

This type of Datatype was specially Created by .NET Framework that is common for all .NET Supported programming languages.

object:-   It can contain all types of elements means the object is the common type in the .NET framework. we can assign int, float, char, and String type data under an object.
object o=10;
object o = "hello";


StringBuilder & String :- both are  used to contain String type data but StringBuilder is a mutable object and String is immutable object, mutable mean, it can change data  dynamically into actual address.


Class:-  it is user define data type,  that  is used to define the characteristics of an real-time object using data member and member function.  it can contain a collection of different types of elements. 

Structure:-  It contains a collection of different datatype variable but it is a value type container of c#.

Enumeration:-  enum is called Constant Array means it can define a set of values, for example, if we want to define a set of five different colors then we can create an enum set.


Array:-   It can contain multiple elements of same type in proper sequence, it can store more than one element using a single variable.



Collection:-   It can store any type of elements, it is an enhancement of an array.

DateTime:-  It can contain Date-time type values.


How to Convert One Data type to another?
           C# provide convert class to convert data type
           String s11 = "123";
           int a11 = Convert.ToInt16(s11); //Numeric String to integer
            double d11 = Convert.ToDouble(s11); //Numeric String to Double
            float f11= Convert.ToSingle(s11); // Numeric String to Float
            float f12 = float.Parse(s11); 
            Console.WriteLine(f12+1);



Most Important Question of C# for an Interview?

What is the boxing and unboxing concept in C#?
     or
What is implicit and explicit conversion in C#?

Answer

Boxing means to convert the value of value type elements to reference type, which means if we convert integer type value to object type then it is called boxing.
Boxing is also called implicit because it will convert the data automatically.


Unboxing means converting reference type elements to value type, which means if we convert object type to integer type then it is called the unboxing concept.
Unboxing is also called explicit conversion because it will convert the data manually.


Example of Boxing and Unboxing using the program?

Memory allocation based on data type?

using System;
class Add
{
   static void Main()
   {
       object o,a=10;
       int b;
       o=a;  // int to object
       b =(int)o;   // object to int
       Console.WriteLine(o + " "+b);
       
   }

}

C# has two different types of memory to store data.

1) Stack Memory:-

It is used to contain the value of int, char, float, double and boolean type elements, it will store data directly hence it is also called direct memory allocation.


2)  Heap Memory:-

It is used to contain the value according to reference String, Object these all are reference type. It is also called Indirect Memory allocation.



ASSIGNMENT?

WAP to calculate simple interest using Object type elements?

WAP to calculate the addition and multiplication of complex numbers?

WAP to calculate Compound Interest?










                              


Comments

  1. Solution of Simple Interest Program:-
    class SIExample
    {
    static void Main()
    {
    object p=25000.23,r=2.2,t=2,si;
    /* float p1 = float.Parse(p.ToString());
    float r1 = float.Parse(r.ToString());
    float t1 = float.Parse(t.ToString());*/
    double p1 = Convert.ToDouble(p);
    double r1 = Convert.ToDouble(r);
    double t1 = Convert.ToDouble(t);
    si = (p1*r1*t1)/100;
    Console.WriteLine("Result of si is {0}",si);


    }


    }

    ReplyDelete
  2. Solution of Simple Interest Program:-
    using System;
    using static System.Array;
    namespace PracticeS
    {
    class Program
    {
    static void Main()
    {
    int p = 12000, r = 3, t = 12;
    int si = (p * r * t)/100;
    Console.WriteLine(si);
    si = Convert.ToInt32(Console.ReadLine());

    }
    }
    }

    ReplyDelete
  3. # Program to implement CI

    class CI
    {
    static void Main()
    {
    double p=50000, r=7, t=2, a;
    int n = 12;
    r = r / 100;
    a = p * Math.Pow(1 + r / n, n * t);

    Console.WriteLine("Total amount after int "+a);
    Console.WriteLine("Actual IR is " + (a - p));

    Console.ReadKey();


    }
    }

    ReplyDelete
  4. //Program to calculate the addition and multiplication of complex numbers.
    using System;
    using System.Numerics;

    class Program
    {
    static void Main()
    {
    Console.WriteLine("Enter the real part of the first complex number:");
    double real1 = Convert.ToDouble(Console.ReadLine());
    Console.WriteLine("Enter the imaginary part of the first complex number:");
    double imag1 = Convert.ToDouble(Console.ReadLine());

    Console.WriteLine("Enter the real part of the second complex number:");
    double real2 = Convert.ToDouble(Console.ReadLine());
    Console.WriteLine("Enter the imaginary part of the second complex number:");
    double imag2 = Convert.ToDouble(Console.ReadLine());

    Complex num1 = new Complex(real1, imag1);
    Complex num2 = new Complex(real2, imag2);

    Complex sum = num1 + num2;
    Complex product = num1 * num2;

    Console.WriteLine($"Addition of {num1} and {num2} = {sum}");
    Console.WriteLine($"Multiplication of {num1} and {num2} = {product}");
    }
    }

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...