Hot File

How to Single Dimensional Arrays In C#

View: 131    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

0 point/1 review File has been tested

Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an array can be accessed by that index.

Introduction

Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an array can be accessed by that index.

It is also called one dimensional array.
This type is most commonly used

General Syntax:

type [ ] array-name = new type[size];

STATEMENT DESCRIPTION
type [ ] array-name Where type can be any data type such as string, int, float, bool etc. These [ ] square brackets are called ranks which indicates that this is a Single Dimensional Array, array-name indicated an array / reference variable name [It can be any meaningful name].
= Assignment operator which assigns an array reference into array-name.
new type [size]

Where new keyword allocates a memory, run-time allocation for an array instance, where type can be any data type such as string, int, float, bool etc. Size specifies the total number of elements which can be store in an array.

 

Example – 1, Single Dimensional Array using foreach loop:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
   // Declaration of a string type single dimensional array which can hold five elements in it.
            string[] programingLanguages = new string[5];
 
   // Statements assign each string element in each individual array index which starts from 0 [zero].
            programingLanguages[0] = "C#";
            programingLanguages[1] = "Asp.net";
            programingLanguages[2] = "Javascript";
            programingLanguages[3] = "Php";
            programingLanguages[4] = "Java";
 
   // foreach loop which loops through each array element and print them.
            foreach (string displayLanguageNames in programingLanguages)
            {
                Console.WriteLine(displayLanguageNames);
            }
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        C#
        Asp.net
        Javascript
        Php
        Java
       */
    }
}
NOTE: Single Dimensional Arrays can also be declared and initialized as following:
string [] programingLanguages = { "C#", "Asp.net", "Javascript", "Php", "Java" };
string [] programingLanguages = new string[5]{ "C#", "Asp.net", "Javascript", "Php", "Java" };

Example – 2, Single Dimensional Array using for loop:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
    // A string type array is declarad and initialized at once.
            string[] programingLanguages = { "C#", "Asp.net", "Javascript", "Php", "Java" };
 
    // for loop which loops through each array element and display them.
            for (int count = 0; count < 5; count++)
            {
                Console.WriteLine(programingLanguages[count]);
            }
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        C#
        Asp.net
        Javascript
        Php
        Java
       */
    }
}

REMEMBER:

foreach loop is only design for iterate the collections, if you want to do extra task with collection, in that situation for loop is preferable instead of foreach

According to preceding examples, if you want to access and display only single value of single dimensional array than you can display it as following

// The output will be: C#
Console.WriteLine(programingLanguages[0]);

 

 

How to Single Dimensional Arrays In C#

How to Single Dimensional Arrays In C# Posted on 09-09-2016  Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an array can be accessed by that index. 5/10 131

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 260    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 262    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 291    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 190    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 339    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 178    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 131    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 145    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd