asp.net C # array operations

Source: Internet
Author: User
Tags foreach arrays

Data is used in every development application, he is a OJB data type Oh, let's take a look at the. NET array, let's take a look at a simple brief application example below.

using System;
Using System.Collections.Generic;
Using System.Text;

Namespace Funwitharrays
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("* * * Fun with arrays");
Simplearrays ();
Arrayinitialization ();
Arrayofobjects ();
Rectmultidimensionalarray ();
Jaggedmultidimensionalarray ();
Passandreceivearrays ();
Systemarrayfunctionality ();
Console.ReadLine ();
}

#region Helper Methods
static void Simplearrays ()
{
Console.WriteLine ("=> Simple Array creation.");
Create and fill an array of 3 integers
int[] myints = new Int[3];
Myints[0] = 100;
MYINTS[1] = 200;
MYINTS[2] = 300;

Now print each value.
foreach (int i in myints)
Console.WriteLine (i);
Console.WriteLine ();
}

static void Arrayinitialization ()
{
Console.WriteLine ("=> array Initialization.");
Array initialization syntax using the new keyword.
string[] Stringarray = new string[] {"One", "two", "three"};
Console.WriteLine ("Stringarray has {0} elements", stringarray.length);

Array initialization syntax without using the new keyword.
Bool[] Boolarray = {false, False, true};
Console.WriteLine ("Boolarray has {0} elements", boolarray.length);

Array initialization with new keyword and size.
int[] Intarray = new Int[4] {20, 22, 23, 0};
Console.WriteLine ("Intarray has {0} elements", intarray.length);
Console.WriteLine ();
}

static void Arrayofobjects ()
{
Console.WriteLine ("=> Array of objects.");

An array of objects can is anything at all.
object[] myobjects = new Object[4];
Myobjects[0] = 10;
MYOBJECTS[1] = false;
MYOBJECTS[2] = new DateTime (1969, 3, 24);
MYOBJECTS[3] = "form & void";

      foreach (Object obj in myobjects)
      {
        //Print the type and value for each item in array.
        Console.WriteLine ("Type: {0}, Value: {1}", Obj.gettype (), obj);
     }
      Console.WriteLine ();
   }
    static void Rectmultidimensionalarray ()
    {
       Console.WriteLine ("=> rectangular multidimensional array.");
     //A rectangular MD array.
      int[,] mymatrix;
      Mymatrix = new int[6, 6];

Populate (6 * 6) array.
for (int i = 0; i < 6; i++)
for (int j = 0; J < 6, J + +)
Mymatrix[i, J] = i * j;

Print (6 * 6) array.
for (int i = 0; i < 6; i++)
{
for (int j = 0; J < 6, J + +)
Console.Write (Mymatrix[i, J] + "T");
Console.WriteLine ();
}
Console.WriteLine ();
}

static void Jaggedmultidimensionalarray ()
{
Jagged multidimensional arrays
Console.WriteLine ("=> jagged Multidimensional Array.");
A jagged MD array (i.e., an array of arrays).
Here we are have an array of 5 different arrays.
int[][] Myjagarray = new int[5][];

Create the jagged array.
for (int i = 0; i < myjagarray.length; i++)
Myjagarray[i] = new Int[i + 7];

Print each row (remember, each element are defaulted to zero!)
for (int i = 0; i < 5; i++)
{
for (int j = 0; J < Myjagarray[i].length; J + +)
Console.Write (Myjagarray[i][j] + "");
Console.WriteLine ();
}
Console.WriteLine ();
}

static void PrintArray (int[] myints)
{
for (int i = 0; i < myints.length; i++)
Console.WriteLine ("Item {0} is {1}", I, myints[i]);
}

Static string[] Getstringarray ()
{
String[] thestrings = {"Hello", "from", "Getstringarray"};
return thestrings;
}

static void Passandreceivearrays ()
{
Console.WriteLine ("=> arrays as params and return values.");
Int[] Ages = {20, 22, 23, 0};
PrintArray (ages);
string[] STRs = Getstringarray ();
foreach (string s in STRs)
Console.WriteLine (s);
Console.WriteLine ();
}

static void Systemarrayfunctionality ()
{
Console.WriteLine ("=> Working with System.Array.");
Initialize items at startup.
String[] Gothicbands = {"Tones on Tail", "Bauhaus", "Sisters of Mercy"};

Print out names in declared order.
Console.WriteLine ("-> is the array:");
Array Upper bounds GetUpperBound
for (int i = 0; I <= gothicbands.getupperbound (0); i++)
{
Print a name
Console.Write (Gothicbands[i] + "");
}
Console.WriteLine ("N");

     //reverse them ...
     /inverse array
      array.reverse (gothicbands);
      Console.WriteLine ("-> the Reversed array");
     //... and print them.
      for (int i = 0; I <= gothicbands.getupperbound (0); i++)
   &nbs p;  {
       //print a name
         Console.Write (Gothicbands[i] + "");
     }
      Console.WriteLine ("N");

     //clear out all but the final member.
      Console.WriteLine ("-> cleared out all but one ...");
      array.clear (gothicbands, 1, 2);
      for (int i = 0; I <= gothicbands.getupperbound (0); i++)
   &nbs p;  {
       //print a name
         Console.Write (Gothicbands[i] + "");
     }
      Console.WriteLine ();
   }
    #endregion
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.