Accessing Arrays Value

In this chapter you will learn:

• How to accessing value from array?
• How to accessing value from array using index position?
• How to accessing value from array using for loop?

After storing values into the array, you need to access the values from array and require some operation on value. As storing value in array is very simple, the accessing value from array is also so simple. You can access array’s value by its index position. Each index position in array refers to a memory address in which your values are saved.

Consider the following example:

If you have to access value from above array then you can access value by following ways:

It is same as:

num1 = 23 * 9; Becaue, arr[1] refers to the value 23 and arr[3] refers to the value 9.

If you are required to access entire value of an array one after another, then you can use loop constructs to iterate through array.

Output

Enter your name to find age:     Jack

Name   :Jack
Age      :34
__

Explanation:

In the preceding program, we create two different types of array. One is integer type array which holds integer value and another one is string type array that holds string value.

Then, we stored name and age in both array and then accept a string value from user to find their corresponding age. We store user input in a find variable. Then, using for loop, we check each index position of name array and compare them with find variable. If they match, the program shows up their name along with age, if they didn’t find any value then shows Not Found!!! message to the user.

Summary

In this chapter you learned how to access value from array using index position as well as loop constructs. In next chapter you will learn about one dimensional array in C#.

More Examples

5 thoughts on “Accessing Arrays Value”

1. Dinesh says:

Can i know how to do it?

output should be like this

Hello Dinesh. Thanks to writing your query to us. Here is your solution.

Solution 1 : a={1,2,3,A,B,C}

This can be simply achieved using `Array.Sort()` Function.

Solution 2 : a= {2,3,1,A,C,B}

It seems it is a random array. You can see the solution here:

C# RANDOMIZE CHAR ARRAY WITH PROGRAMMING EXAMPLE

2. Hi admin, can you help me to find out what is wrong with my code. I input all the names and ages but when i find the name through age ,it ended up “Not Found”.

1. Ibrahim says:

@jerry, from your code, you re to declare find as integer and when finding age, you are to convert the user input. i think this will help. Am also a newbie in it.

3. sushil k jena says:

this is best site for learning purpose.A beginner can understand easily…thank u admin