How To Handle Array File And Exception In C#.Net
Rate this article
0 out of 5
In this tutorial we will show you how to handle array file and exception using C#.Net. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.

Source Code

using System;
using System.Collections; 
using System.IO; 
using System.Text; 

namespace ArrayFileAndExceptionHandlingExample
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Person //Create a person class with basic properties and methods
public int iAge; //Property
private string strName; 
public string GetName()//Method
return strName;
public void SetName(string Name)
public virtual void Play() //Virtual function
System.Console.WriteLine("I plays Nothing");  

class ArrayFileAndExceptionHandling
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
//Create a person Array
Person[] PersonArray= new Person[3] ; //Create an array of 3 persons
PersonArray[0]= new Person(); //Create theperson 1
PersonArray[0].SetName("Jerry"); //set the name
PersonArray[1]= new Person(); 
PersonArray[2]= new Person(); 
System.Console.WriteLine("Using Arrays" );
//Iterate through each element in the array using the foreach loop
foreach ( Person i in PersonArray )  
System.Console.WriteLine(i.GetName() );   
//Using ArrayList collection class
ArrayList PersonsList= new ArrayList(); //In namespace System.Collections
Person Person1= new Person();
Person Person2= new Person();
System.Console.WriteLine("Using Arraylist" );
foreach ( Person List in PersonsList )  
System.Console.WriteLine(List.GetName() );   
//Save these details to a file PersonDetails.txt
try //Exception handler
System.Console.WriteLine("Saving the details to the file" );
using (FileStream fs = File.Create("PersonDetails.txt")) //Creating the file with the specified name
// Add some information to the file.
foreach ( Person List in PersonsList )  
byte[] bValues =new UTF8Encoding(true).GetBytes(List.GetName()); //Extracting the bytes

fs.Write(bValues, 0, bValues.Length);//Writing the contents to the file   

catch(Exception ex) //catch block 
System.Console.WriteLine("Some Error Occour while saving :" +ex.Message );

Source Code

About The Author
KTS Training

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.

Number Of articles Posted
Number Of Messages Posted
Joined Date
Post Your Comments
Name (Max 50 Chars)

Academic Projects