working with arrays



working with arrays..

Answer / kris

Initializing Arrays

C# provides simple and straightforward ways to initialize
arrays at declaration time by enclosing the initial values
in curly braces ({}). The following examples show different
ways to initialize different kinds of arrays.

Note If you do not initialize an array at the time of
declaration, the array members are automatically initialized
to the default initial value for the array type. Also, if
you declare the array as a field of a type, it will be set
to the default value null when you instantiate the type.

Single-Dimensional Array

int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne", "Robert"};

You can omit the size of the array, like this:

int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};

You can also omit the new operator if an initializer is
provided, like this:

int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Matt", "Joanne", "Robert"};

Multidimensional Array

int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Mike","Amy"},
{"Mary","Albert"} };

You can omit the size of the array, like this:

int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"},
{"Mary","Albert"} };

You can also omit the new operator if an initializer is
provided, like this:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };

Jagged Array (Array-of-Arrays)

You can initialize jagged arrays like this example:

int[][] numbers = new int[2][] { new int[] {2,3,4}, new
int[] {5,6,7,8,9} };

You can also omit the size of the first array, like this:

int[][] numbers = new int[][] { new int[] {2,3,4}, new int[]
{5,6,7,8,9} };

-or-

int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

Notice that there is no initialization syntax for the
elements of a jagged array.
Accessing Array Members

Accessing array members is straightforward and similar to
how you access array members in C/C++. For example, the
following code creates an array called numbers and then
assigns a 5 to the fifth element of the array:

int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4] = 5;

The following code declares a multidimensional array and
assigns 5 to the member located at [1, 1]:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;

The following is a declaration of a single-dimension jagged
array that contains two elements. The first element is an
array of two integers, and the second is an array of three
integers:

int[][] numbers = new int[][] { new int[] {1, 2}, new int[]
{3, 4, 5}
};

The following statements assign 58 to the first element of
the first array and 667 to the second element of the second
array:

numbers[0][0] = 58;
numbers[1][1] = 667;

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More C Sharp Code Interview Questions

How to Create a Treeview Menu in ASP.NET with C#?

1 Answers  


Create a class called Accounts which has data members like ACCOUNT no, Customer name, Account type, Transaction type (d/w), amount, balance D->Deposit W->Withdrawal If transaction type is deposit call the credit(int amount) and update balance in this method. If transaction type is withdraw call debit(int amt) and update balance. Pass the other information like Account no,name,Account Type through constructor. Call the show data method to display the values.

1 Answers   Cognizant,


how do i copy textbox contents of 1 form to another form

4 Answers   Wipro,


Write a program to count 3Letter, 4Letter and 5Letter words from a file and print the number of 3Letter, 4Letter and 5Letter words. Delimiter is space, tab, hifen. Also we should not consider the line in the file after we encounter # in that line.

0 Answers   Mind Tree,


<ul> <li id="Accommodation" runat="server" visible="false"><a href="../Elements/frmSearchAccommodation.aspx?submenuheader=1">Accommodation</a></li> <li id="Arts" runat="server" visible="false"><a href="../Elements/frmSearchProp.aspx?submenuheader=1">Arts And Props</a></li> <li id="Costume" runat="server" visible="false"><a href="../Elements/frmSearchCostume.aspx?submenuheader=1">Costume</a></li> <li id="Crew" runat="server" visible="false"><a href="../Elements/frmSearchCrew.aspx?submenuheader=1">Crew</a></li> <li id="Catering" runat="server" visible="false"><a href="../Elements/frmSearchCatering.aspx?submenuheader=1">Catering</a></li> <li id="Equipment" runat="server" visible="false"><a href="../Elements/frmSearchEquipment.aspx?submenuheader=1">Equipment</a></li> <li id="Film" runat="server" visible="false"><a href="../Elements/frmSearchFilmTapeStock.aspx?submenuheader=1">Film And Tape Stocks</a></li> <li id="Location" runat="server" visible="false"><a href="../Elements/frmSearchLocation.aspx?submenuheader=1">Location</a></li> <li id="Picture" runat="server" visible="false"><a href="../Elements/frmSearchPictureVehicle.aspx?submenuheader=1">Picture Vehicles</a></li> <li id="Production" runat="server" visible="false"><a href="../Elements/frmSearchProductionSupplies.aspx?submenuheader=1">Production Supplies</a></li> <li id="Post" runat="server" visible="false"><a href="../Elements/frmSearchPostProductionHouse.aspx?submenuheader=1">Post Production Houses</a></li> <li id="Rigging" runat="server" visible="false"><a href="../Elements/frmSearchRigging.aspx?submenuheader=1">Rigging</a></li> <li id="Stunt" runat="server" visible="false"><a href="../Elements/frmSearchStunt.aspx?submenuheader=1">Stunt</a></li> <li id="Studio" runat="server" visible="false"><a href="../Elements/frmSearchStudio.aspx?submenuheader=1">Studio</a></li> <li id="SFX" runat="server" visible="false"><a href="../Elements/frmSearchSFX.aspx?submenuheader=1">SFX</a></li> <li id="Talents" runat="server" visible="false"><a href="../Elements/frmSearchTalent.aspx?submenuheader=1">Talents</a></li> <li id="Transport" runat="server" visible="false"><a href="../Elements/frmSearchTransport.aspx?submenuheader=1">Transport</a></li> <li id="Venue" runat="server" visible="false"><a href="../Elements/frmSearchVenue.aspx?submenuheader=1">Venue</a></li> <li id="Other" runat="server" visible="false"><a href="../PageUnderConstruction.aspx?submenuheader=1">Other</a></li> </ul> I have this code in leftmenu.ascx page..now on leftmenu.ascx.cs page i want to access id values. but i dont want to check manually like Accommodation,Arts etc.. In cs file my code is like string str[]; //this array contains some values say 10 strings for(i=0;i<100;i++) { if(str[i]==id id.visible=true } i.e.i want to travel for each element of str and each value of id.if they match then id.visible=true how to do?

0 Answers  






Write a program to count the number of characters, number of words, number of line in file.

2 Answers   Mind Tree,


How to export 2 datatables of a single dataset to 2 different worksheets of a single MSExcel file ?

0 Answers   Eastcom Systems,


How to Link Different Data Sources Together?

0 Answers  


Event Handling in C# Triggering a Button

1 Answers  


How to Create Files by Using the FileInfo Class?

1 Answers  


program for straight line(y=mx+c)

0 Answers   Mind Tree,


Code for Reading and writing from a file in c#?

1 Answers  


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)