C Sharp/Arrays II

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Home

Basics
Home
introduction
Overview
Statements
Data Types
Variables
Operators
Flow Control
Variables II
Functions
C# - Classes and Objects I

Dates and Times
Conversions

Advanced
Classes II
Inheritance
GUI
Namespaces
Exceptions
Threads
ADO.NET
Generics

Contents

[edit] Pass Arrays to Methods and Functions

[edit] Passing Arrays to Functions

In some casses you may find it necessary to pass arrays to methods and functions. To do this declare the array

int[] x = { 1, 45, 155, 78, 12 };

then call the method like this

MyFunction(x);

When calling the method it is not required to send the array size. The array already its array size stored. Below is an example of how to pass an array to a method or function

using System;
 
class Program
{
    static void MyFunction(int [] a)
    {
        for (int b = 0; b < a.Length; b++)
            Console.WriteLine(a[b]);
        Console.WriteLine("You are in the function");
    }
    static void Main(string[] args)
    {
        int[] x = { 1, 45, 155, 78, 12 };
 
        MyFunction(x);
        Console.Read();
    }
}

Output

1
45
155
78
12
You are in the function


[edit] Passing Arrays to Methods

Passing arrays to methods works the same way as passing arrays to functions. There is a little syntax differents because a class object must be created.

using System;
 
class Program
{
    class MyClass
    {
        public void MyMethod(int[] a)
        {
            for (int b = 0; b < a.Length; b++)
                Console.WriteLine(a[b]);
            Console.WriteLine("You are in the function");
        }
    }
    static void Main(string[] args)
    {
        int[] x = { 1, 45, 155, 78, 12 };
        MyClass MyObject = new MyClass();
        MyObject.MyMethod(x);
        Console.Read();
 
 
    }
}

Output

1
45
155
78
12
You are in the function


[edit] Passing Arrays by Value and Reference

Passing arrays by reference has a great performance benefit. Pass arrays by reference anytime you get because the compiler does not have to create a copy of the array it only has to create a reference (pointer) to the array which is a whole lot faster. What if an array had 1 million elements it will take a noticable delay to create copies of all those elements. You can avoid all this by passing arrays by reference. Here is an example:

The examplebelow

using System;
 
class Program
{
    class MyClass
    {
        public void CallByRef(ref int[] a)
        {
            for (int b = 0; b < a.Length; b++)
                Console.WriteLine(a[b]);
            Console.WriteLine("Call by reference");
        }
 
        public void CallByVal(int[] a)
        {
            for (int b = 0; b < a.Length; b++)
                Console.WriteLine(a[b]);
            Console.WriteLine("Call by value");
        }
    }
    static void Main(string[] args)
    {
        int[] x = { 1, 45, 155, 78, 12 };
        MyClass MyObject = new MyClass();
        MyObject.CallByRef(ref x);
 
        Console.WriteLine("");
 
        MyObject.CallByVal(x);
        Console.Read();
 
 
    }
}

Output

1
45
155
78
12
Call by reference

1
45
155
78
12
Call by value