Pages

Friday, May 10, 2013

array in c#

Array is a similar data or It is data type

    To store collection of related data,we should always try to use Arrays.

    It will reduce the amount of memory consume by storing the data.


Types of array:

1.one dimensional array

2.Multi dimensional array

3.jagged array


Syntax :

 Datatype [] variablename(user define)=new Datatype[size]{data};


Example:

string [] sname=new string[4]{"rakesh","ritesh","surav","shakti"};  // for string declare


Example:

 bool[]  gender =new bool[4]{"true","false",true","false"}; // for boolean deaclare


Example:

 char [] curency =new charl[4]{'$','&',*',@'}; // for character declare


Array using loop:


{

 int [] sal =new int[4]{500,400,300,700};

for(int i=0;i<4;i++)

     {

            int r=sal[i];

           response.write(r);

    }

}

Question: How to modify array?

ans:  

 int [] sal =new int[4]{500,400,300,700};

  sal[3]=100;

  sal[0]=800;

note: here the new value will be come 800,400,300,100 bec old value will be replace and new value will be come.{ 0 and 3} array array location and array always start zero(0);

Simple program:(calculate avg marks)

public partial class _Default:System.Web.UI.Page
{
    protected void Page_Load(object sender,EventArgs e)
  {
     int[] fsm=new int[3]{70,80,90};
      int[] ssm=new int[3]{80,70,90};
      MrkCalc m=new MrkCalc();
      int[] res=m.Avgmarks(fsm,ssm);
      for(int i=0;i<res.Length;i++)
      {
           Response.Write(res[i]);
      }
   }
}

public class MrkCalc
    {
     public int[] Avgmarks(int[] fs1,int[] ss1)
        {
           int[] res=new int[fs1.Length];
            for(int i=0;i<res.Length;i++)
         {
           res[i]=(fs1[i]+ss1[i])/2;
         }
           return res;
  }
}

No comments:

Post a Comment