التخطي إلى المحتوى الرئيسي

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?










                              


تعليقات

  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);


    }


    }

    ردحذف
  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());

    }
    }
    }

    ردحذف
  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();


    }
    }

    ردحذف
  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}");
    }
    }

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...