+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 40

Thread: cegonsoft profile | Arrays in C# .net

  1. #1

    Default cegonsoft profile | Arrays in C# .net

    Arrays allow you to store a series of values that have the same data type. Each individual value in the array is accessed using one or more index numbers. It’s often convenient to picture arrays as lists of data (if the array has one dimension) or grids of data (if the array has two dimensions).

    All arrays start at a fixed lower bound of 0. This rule has no exceptions. When you create an array in C#, you specify the number of elements. Because counting starts at 0, the highest index is actually one fewer than the number of elements. (In other words, if you have three elements, the highest index is 2.)

    // Create an array with four strings (from index 0 to index 3).
    // You need to initialize the array with the
    // new keyword in order to use it.

    string[] stringArray = new string[4];

    // Create a 2x4 grid array (with a total of eight integers).

    int[,] intArray = new int[2, 4];

    By default, if your array includes simple data types, they are all initialized to default values (0 or false), depending on whether you are using some type of number or a Boolean variable.

    You can also fill an array with data at the same time that you create it. In this case, you don’t need to explicitly specify the number of elements, because .NET can determine it automatically:

    // Create an array with four strings, one for each number from 1 to 4.

    string[] stringArray = {"1", "2", "3", "4"};

    The same technique works for multidimensional arrays, except that two sets of curly brackets are required:

    // Create a 4x2 array (a grid with four rows and two columns).

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

    Visit: Cegonsoft

  2. #2

    Default

    Array Initialization Syntax

    To fill the items of an array using C# array initialization syntax, we need to specify each array item within the scope of curly brackets {}. This syntax can be helpful when we are creating an array of a known size and want to quickly specify the initial values. Let's look at the following alternative array declaration:

    // Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MyFirstCSharpCode
    {
    class Program
    {
    static void ArrayInitialization()
    {
    string[] stringArray = new string[] { "Ludwig", "van", "Beethoven" };
    Console.WriteLine("stringArray has {0} elements", stringArray.Length);

    bool[] boolArray = { false, false, true };
    Console.WriteLine("boolArray has {0} elements", boolArray.Length);

    int[] intArray = new int[5] { 1, 2, 3, 4, 5 };
    Console.WriteLine("intArray has {0} elements", intArray.Length);
    Console.WriteLine();
    }

    static void Main(string[] args)
    {
    ArrayInitialization();
    Console.ReadLine();
    }
    }
    }

    Output is:

    stringArray has 3 elements
    boolArray has 3 elements
    intArray has 5 elements

    When we make use of the curly bracket syntax, we do not need to specify the size of the array as in the stringArray type, given that this will be inferred by the number of items within the scope of the {}. Also note that use of the new keyword is optional as shown when we construct the boolArray type in the example.

    In the case of the intArray declaration, the numeric value specified represents the number of elements in the array, not the value of the upper bound. If there is a mismatch between the declared size and the number of initializers, we'll get a compile error:

    int[] array = new int[2] {1, 2, 3, 4}; // error: mismatch



    Array of Objects

    When we define an array, we specify the type of item that can be within the array variable. While this seems quite straightforward, there is one notable twist. System.Object is the ultimate base class to each and every type including fundamental data types in the .NET type system. If we were to define an array object, the subitems could be anything at all. Take a look at the following ArrayOfObjects() method:

    // Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MyFirstCSharpCode
    {
    class Program
    {
    static void ArrayOfObjects()
    {
    object[] myObj = new object[4];
    myObj[0] = 1;
    myObj[1] = false;
    myObj[2] = new DateTime(2010, 11, 25);
    myObj[3] = "Ludwig van Beethoven";

    foreach (object o in myObj)
    {
    Console.WriteLine("Type: {0}, value: {1}", o.GetType(), o);
    }
    Console.WriteLine();
    }

    static void Main(string[] args)
    {
    ArrayOfObjects();
    Console.ReadLine();
    }
    }
    }

    Output from the run is:

    Type: System.Int32, value: 1
    Type: System.Boolean, value: False
    Type: System.DateTime, value: 11/25/2010 12:00:00 AM
    Type: System.String, value: Ludwig van Beethoven

    In the example, as we are iterating over the contents of myObj, we print out the underlying type of each item using the GetType() method of System.Object as well as the value of the current item. Note that GetType() can be used to obtain the fully qualified name of the item.

    Multidimensional Arrays

    C# also supports two varieties of multidimensional arrays. The first one is a rectangular array which is an array of multiple dimensions, where each row is of the same length. Let's look at the following example.

    // Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MyFirstCSharpCode
    {
    class Program
    {
    static void RectangularArray()
    {
    int[,] myRectArr;
    myRectArr = new int[6,6];

    for(int i = 0; i < 6; i ++)
    for(int j = 0; j < 6; j++)
    myRectArr[i, j] = i * j;

    for(int i = 0; i < 6; i ++)
    {
    for(int j = 0; j < 6; j++)
    Console.Write(myRectArr[i, j] + " ");
    Console.WriteLine();

    Console.WriteLine();
    }

    static void Main(string[] args)
    {
    RectangularArray();
    Console.ReadLine();
    }
    }
    }

    Output is:

    0 0 0 0 0 0
    0 1 2 3 4 5
    0 2 4 6 8 10
    0 3 6 9 12 15
    0 4 8 12 16 20
    0 5 10 15 20 25

    The other type of multidimensional array is a jagged array. Jagged arrays contain some number of inner arrays, each of which may have a unique upper limit:

    // Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MyFirstCSharpCode
    {
    class Program
    {
    static void myJaggedArray()
    {
    int[][] myJaggedArr = new int[5][];

    for (int i = 0; i < myJaggedArr.Length; i++)
    {
    myJaggedArr[i] = new int[i + 7];
    }

    for(int i = 0; i < 5; i ++)
    {
    for(int j = 0; j < myJaggedArr[i].Length; j++)
    Console.Write(myJaggedArr[i][j] + " ");
    Console.WriteLine();
    }
    Console.WriteLine();
    }

    static void Main(string[] args)
    {
    myJaggedArray();
    Console.ReadLine();
    }
    }
    }

    Output is:

    0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0



    Arrays for Parameters and Return Values

    Once we have created an array, we are free to pass it as a parameter and receive it as a return value. In the following example, PrintArray() method takes an incoming array of ints and prints each while the GetStringArray() method populates an array of strings it to the caller.

    // Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MyFirstCSharpCode
    {
    class Program
    {
    static void PrintArray(int[] myIntArr)
    {
    for (int i = 0; i < myIntArr.Length; i++)
    {
    Console.WriteLine("myIntArr[{0}] = {1}", i, myIntArr[i]);
    }
    }

    static string[] GetStringArray()
    {
    string[] strs = {"imperative", "declarative", "functional"};
    return strs;
    }

    static void PassAndReceiveArrays()
    {
    int[] fibo = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };
    PrintArray(fibo);

    string[] myStrings = GetStringArray();
    foreach (string s in myStrings)
    Console.WriteLine(s);
    Console.WriteLine();
    }

    static void Main(string[] args)
    {
    PassAndReceiveArrays();
    Console.ReadLine();
    }
    }
    }

    Output from the run is:

    myIntArr[0] = 0
    myIntArr[1] = 1
    myIntArr[2] = 1
    myIntArr[3] = 2
    myIntArr[4] = 3
    myIntArr[5] = 5
    myIntArr[6] = 8
    myIntArr[7] = 13
    myIntArr[8] = 21
    imperative
    declarative
    functional

    this is the arrays in framework 4.0 .... cegonsoft

  3. #3

    Default

    A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.

    The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:

    C#

    int[][] jaggedArray = new int[3][];

    Cegonsoft

  4. #4

    Default

    using System;
    using System.Collections;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[] numbers = { 4, 3, 8, 0, 5 };

    Array.Sort(numbers);

    foreach(int i in numbers)
    Console.WriteLine(i);

    Console.ReadLine();
    }
    }
    }

    The only real new thing here is the Array.Sort command. It can take various parameters, for various kinds of sorting, but in this case, it simply takes our array. As you can see from the result, our array has been sorted. The Array class has other methods as well, for instance the Reverse() method. You can look it up in the documentation to see all the features of the Array class.
    cegonsoft

  5. #5

    Default

    PHP is a scripting language commonly used on web servers.
    PHP is the Open source technology it is used in the most of the companies.
    So PHP training is the very important one.
    In cegonsoft they provide very good industry based training for the PHP, so that i shine in my company very well thanks for the cegonsoft.

    Cegonsoft

  6. #6

    Default

    Hi,
    Cegonsoft is the best IT Training Centre providing training on various technologies including .NET in Karnataka and Tamilnadu.

    Their comprehensive .NET course provides in-depth coverage of all .NET features from fundamental to advanced topics.
    They also provide Placement Assistance for their trainees and International Certification after completion of Course.


    Cegonsoft

  7. #7

    Default

    It's nice example array program in c#.
    Cegonsoft

  8. #8

    Default

    This simple tutorial teaches you how to work with an ArrayList. Many people who pass the C / C + + of the world is that there is no dynamic memory allocation tables in C #. In fact, this is a kind of NET, which allows you the same features as dynamic memory allocation, but it is much easier to use -. The ArrayList class. With an ArrayList, you will not have to worry about freeing memory after memory allocation and array bound overflow.

    class TestArrayList {
    public static void Main() {
    ArrayList myList = new ArrayList();
    int i;

    for(i=0; i<5; i++)
    myList.Add(i);

    myList.Add("Hello");

    for(i=0; iConsole.WriteLine("Array Index [{0}]: {1}", i, myList[i].ToString());
    }
    }


    To use it you must "use" the namespace System.Collections ArrayList class that is defined in it. Add () method accepts an ArrayList object instance as an argument, then, as you can see, you can put in an integer or a string of the same, or any user defined class.

    cegonsoft

  9. #9
    Emerald Star member madhusundar is on a distinguished road
    Join Date
    Sep 2011
    Posts
    104

    Default C# and dot net training in CEGONSOFT

    C# is Microsoft's new programming language for the .NET platform.
    It combines some of the best features of modern programming languages such as Java, C++ or Visual Basic.
    C# is an object-oriented language with single inheritance but multiple interfaces per class.
    It supports component-based programming by properties (smart fields), events and delegates (enhanced function pointers).

    The .NET framework created by Microsoft is a software development platform that focuses on rapid application development, platform independence and network transparency.

    .NET is Microsoft's strategic initiative for server and desktop development for the next decade.
    According to Microsoft, .NET includes many technologies that are designed to facilitate rapid development of Internet and Intranet applications.



    CEGONSOFT is one of the country's fastest growing Information Technology Solution Company. CEGONSOFT has established itself with three independent successful divisions.

    Software Training
    Software Development
    HR Consultancy
    Providing International Certifications after the course and job assistance will be done through the HR.




    CEGONSOFT
    Last edited by madhusundar; 10-06-2011 at 09:53 AM.

  10. #10

    Default

    Cegonsoft provides the knowledge to the students to achieve a great career in their life. Cegonsoft is always ready to take challenge for the students to be known around the world. Because Cegonsoft gives world-class training to freshres and job seekers. Cegonsoft not only gives the training but also nice placements as well.

    Cegonsoft

+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts