Showing posts with label c sharp. Show all posts
Showing posts with label c sharp. Show all posts

Thursday, October 22, 2020

Simple DataTable example in c sharp.

 




What are data tables?

DataTable represents data in table format as rows and columns.it is used for disconnected data access.

Dataset and data view both uses DataTable.

Create  sample Data table.

 I think following code is self explanatory.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data;

 

namespace DataTable

{

    class Program

    {

        static void Main(string[] args)

        {

            //create new Data table

 

            System.Data.DataTable dt = new System.Data.DataTable("stud");

            DataColumn col;

 

            // create three columns in Data table

           

            col = new DataColumn();

            col.DataType = System.Type.GetType("System.Int32");

            col.ColumnName = "Id";

            col.ReadOnly = true;

            col.Unique = true;

            dt.Columns.Add(col);

 

            col = new DataColumn();

            col.DataType = System.Type.GetType("System.String");

            col.ColumnName = "Name";

            col.ReadOnly = false ;

            col.Unique = false;

            dt.Columns.Add(col);

 

            col = new DataColumn();

            col.DataType = System.Type.GetType("System.Int32");

            col.ColumnName = "Mark";

            col.ReadOnly =false;

            col.Unique = false;

            dt.Columns.Add(col);

 

 

            // create primary key

 

            DataColumn[] PrimaryKeyColumns = new DataColumn[1];

            PrimaryKeyColumns[0] = dt.Columns["Id"];

            dt.PrimaryKey = PrimaryKeyColumns;

 

           

 

            // create rows

 

            dt.Rows.Add(1, "karthieyan", 75);

            dt.Rows .Add(2, "Arun", 80);

            dt.Rows.Add(3, "hari", 78);

 

             // read rows from datatable

            foreach (DataRow tr in dt.Rows)

            {

                Console.WriteLine("Id={0}, Name={1}, Mark={2}", tr["Id"], tr["Name"], tr["Mark"]);

 

            }

 

            Console.ReadLine();

 

 

 

 

        }

    }

}

 

Output:



 

Thank you.

Muthu karthikeyan, Madurai.

Contact:

91 96293 29142

Tamil nad, India

 

 

Diff between value type and reference type in c sharp.

 



C # types are generally come under two groups.

One is value type.

Another is reference type.

Value type datas are stored in stack memory and reference type datas are stored in heap memory.

Value type variable store the data directly stores the data in memory location .

Reference type values are store in one location and from another location it points to the location of original data. i.e values are stored in one location and its address are stored in another location.

While you copy a value type to another value type variable both act independently because they are treated as entirely different elements.

Example:

Int x,y;

X=10;

Y=x;

Now  x and y will both have 10.

X=20;

Now x will contain 20 and y will still hold 10.

Because of they are treated as different variables change in one variable doesn’t affect the content of another variable.


 

Reference type variables act differently.

When you copy from one variable to another variable of reference type both will point to same location.

Ie both will store the address of  same location of data.

So when you alter the original value because of both points to the same location change in one reference type data will also change the data of another variable.

Thank you.

Muthu karthikeyan,Madurai.

91 96293 29142 

Tamil nad, india


 

 

Boxing and unboxing in c#.



In c# there are two type of datas .one is value type and other is reference types.

Variables of different data types

string stringVar = "Hello";

int intVar = 500;

float floatVar = 20.2f;

char charVar = 'E';

bool boolVar = true;

 

value types include simple types such as int,float,double, Boolean or enum, struct types.

Reference type includes  class,array,interface, delegate and array.

Boxing means converting from value type to reference type.

Unboxing means converting from reference  type to value type.

Boxing:

  1. class Test  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         int i = 1;  
  6.         object o = i; // boxing  
  7.         int j = (int)o; // unboxing  
  8.     }  
  9. }  

 

In the above example 1 is assigned to value type variable  i.

This I value is assigned to reference type o.

This is called boxing.

Object o is type casted into type int and then assigned to value type int named j.

This is unboxing.

Note when we do boxing we directly assign the value while in unboxing there is an explicit type cast is required.