Loading...

How to Single Dimensional Arrays In C#

View: 450    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

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#

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.

Posted on 09-09-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Sound Player in Visual Studio C++ .NET

    Sound Player in Visual Studio C++ .NET

    View: 0    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This sound player project use the libZPlay from http://libzplay.sourceforge.net . Methods are called from library libzplay.dll with [InteropServices::DllImport("")]. The use of the library is relatively easy.

  • Quiz Management System in C++

    Quiz Management System in C++

    View: 5    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Hello World...!!! Though I am not an expert programmer but today i want to share my little knowledge of C/C++ programming by sharing this simple Quiz Management System with you people. I hope this may help some students in their mini...

  • General Quiz

    General Quiz

    View: 77    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is one of my first WPF projects, and it may be used as a template for a quiz test. The code can be improved but I think its useful for the ones who don't have experience with WPF.

  • Calculating Average and Grade

    Calculating Average and Grade

    View: 25    Download: 2   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Calculating Average and Grade is a small program in order to find the result of students in the class.

  • H.K.M.S SUPREME TYRES

    H.K.M.S SUPREME TYRES

    View: 12    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    H.K.M.S SUPREME TYRES --------------------- Goals: --------- * Brand Master * Size Master * User Master * Customer Master * Purchase Form * Order Form * Bill Form Software is as follows: ------------------------------ * Microsoft Visual Studio 2013 *...

  • Cronpp - a C++ library for CRON expressions

    Cronpp - a C++ library for CRON expressions

    View: 12    Download: 0   Comment: 0

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

    A CRON expression is a string composed of six fields (in some implementation seven), separated by a whites space, representing a time schedule. CRON expressions are used in various job schedulers (such as the Linux job scheduler, the Quartz...

  • Departmental store management system

    Departmental store management system

    View: 19    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is a simple project on departmental store management system developed in c++ programming language . This project helps to keep all the information details of the items available in departmental store and maintains the stock and prints the bill....

  • Volume of a Cone in C

    Volume of a Cone in C

    View: 23    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Code Features: 1. Find the Volume of a cone 2. Find the value of other properties of the cone 3. Teach user how to compute the volume of the cone manually Thanks to following for the code: John Carlo C. Nañoz, Dondon Bagasbas, Gabby Peñones

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd