Archive for June, 2015

Below is the code to initialise char array 1D :


using System;

namespace ConsoleApplication1
 {
 class Program
 {
 static void Main(string[] args)
 {
 //int i = 0;
 char[] array = new char[5];
 for (int i = 0; i < 4; i++)
 {
 array[i] = char.Parse(Console.ReadLine());
 }
 Console.WriteLine("-------");
 for (int i = 0; i < 4; i++)
 {
 Console.WriteLine(array[i]);
 }

Console.ReadLine();

}
 }
 }

output :

NOTE: 

http://stackoverflow.com/questions/230454/how-to-fill-an-array-from-user-input-c

Exception :

When we input string the exception occurs

Below is the simple code to display 1D array :

using System;
using System.Data.OleDb;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//int i = 0;
int[] array = new int[5];
for (int i = 0; i < 4; i++)
{
array[i] = Int32.Parse(Console.ReadLine());
}
Console.WriteLine(“——-“);
for (int i = 0; i < 4; i++)
{
Console.WriteLine(array[i]);
}

Console.ReadLine();

}
}
}

Below is the simple script to a collection (arraylist – > DataTable) :

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace Datatable
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add(“The”);
al.Add(“quick”);
al.Add(“brown”);
al.Add(“fox”);

// Create new DataTable.
DataTable table = new DataTable(“table”);

// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;

// Create column.
column = new DataColumn();
column.DataType = Type.GetType(“System.String”);
column.ColumnName = “item”;
table.Columns.Add(column);

// Add items from ArrayList to DataTable.
foreach (String item in al)
{
row = table.NewRow();
row[“item”] = item;
table.Rows.Add(row);
}
}
}
}