array

This is a discussion on array within the C# Programming forums, part of the General Programming Boards category; high i m beginner of c# languages & i dont know how it works . so pls send the syntax ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    array

    high i m beginner of c# languages & i dont know how it works . so pls send the syntax of arrays.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    search a array

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Hello Basant.

    As you are a beginner to C# I would like to point out that Arrays are fixed length and Lists are dynamic length. More often you would want to use Lists unless you are dealing with constant variables predetermined. However here is a little demonstration, showing some Array syntax and examples, and then finally how to search by condition without the hassle of using for loops.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 5 element int array (pre-populated)
                int[] number_array1 = new int[] { 1, 2, 3, 4, 5 };
    
                // 5 element int array (empty)
                int[] number_array2 = new int[5];
    
                // 2 element string array (pre-populated)
                string[] string_array1 = new string[] { "foo", "bar" };
    
                // 2 element MyObject array (pre-populated)
                MyObject[] myobject_array1 = new MyObject[]
                {
                    new MyObject
                    {
                        Name = "foo",
                        Age = 20
                    },
                    new MyObject
                    {
                        Name = "bar",
                        Age = 25
                    }
                };
    
                // search for a MyObject where his age is 25
                MyObject result = myobject_array1.FirstOrDefault<MyObject>(x => x.Age == 25);
    
                if (result != null)
                    Console.WriteLine("{0} is 25 years old", result.Name);
                else
                    Console.WriteLine("no one in the array is 25");
    
                Console.Read();
            }
        }
    
        class MyObject
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
    Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 10:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21