KEMBAR78
CSharp_02_Arrays_fundamentals_concepts_introduction | PPT
Programming in C#
Arrays and Indexers
Arrays and Indexers
CSE 494R
(proposed course for 459 Programming in C#)
Prof. Roger Crawfis
Array Declarations
 An array is an object (not just a stream of
objects). See System.Array.
 Bounds checking is performed for all access
attempts.
 Declaration similar to Java, but more strict.
 Type definition: a is a “1D array of int’s”
 Instance specifics: a is equal to a 1D array of int’s
of size 10.
int[] a = new int[10];
int[] b = a;
int[] c = new int[3] {1,2,3};
int[] d = {1,2,3,4};
Jagged Arrays
Can have standard C-style jagged arrays
int[] array = new int[30];
int[][] array = new int[2][];
array[0] = new int[100];
array[1] = new int[1];
 Stored in random parts of the heap
 Stored in row major order
C# Arrays
 Multi-dimensional arrays are jagged arrays
with a user-enforced constraint in C.
 Really just 1D arrays, but each element can contain
a 1D array (recursion).
 C# provides true multi-dimensional arrays
 Elements are stored sequentially
 CLR (JIT compiler) computes the offset code
int[,] array = new int[10,30];
array[3,7] = 137;
int[,] arr4 = new int [2,3] { {1,2,3}, {4,5,6} };
int[,] arr5 = new int [,] { {1,2,3}, {4,5,6} };
int[,] arr6 = { {1,2,3}, {4,5,6} };
Indexers
Allow bracket notation on any object
public string this[int a, double b]
{ … }
Related to C++ operator[ ] overloading
Special property
public class ListBox: Control {
private string[] items;
public string this[int index] {
get { return items[index]; }
set { items[index] = value;
Repaint(); }
}
}
ListBox listBox = new ListBox();
listBox[0] = "hello";
Console.WriteLine(listBox[0]);
Indexers
 Lets an instance behave as a virtual array
 Can be overloaded
 Can be read-only, write-only, or read/write

CSharp_02_Arrays_fundamentals_concepts_introduction

  • 1.
    Programming in C# Arraysand Indexers Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
  • 2.
    Array Declarations  Anarray is an object (not just a stream of objects). See System.Array.  Bounds checking is performed for all access attempts.  Declaration similar to Java, but more strict.  Type definition: a is a “1D array of int’s”  Instance specifics: a is equal to a 1D array of int’s of size 10. int[] a = new int[10]; int[] b = a; int[] c = new int[3] {1,2,3}; int[] d = {1,2,3,4};
  • 3.
    Jagged Arrays Can havestandard C-style jagged arrays int[] array = new int[30]; int[][] array = new int[2][]; array[0] = new int[100]; array[1] = new int[1];  Stored in random parts of the heap  Stored in row major order
  • 4.
    C# Arrays  Multi-dimensionalarrays are jagged arrays with a user-enforced constraint in C.  Really just 1D arrays, but each element can contain a 1D array (recursion).  C# provides true multi-dimensional arrays  Elements are stored sequentially  CLR (JIT compiler) computes the offset code int[,] array = new int[10,30]; array[3,7] = 137; int[,] arr4 = new int [2,3] { {1,2,3}, {4,5,6} }; int[,] arr5 = new int [,] { {1,2,3}, {4,5,6} }; int[,] arr6 = { {1,2,3}, {4,5,6} };
  • 5.
    Indexers Allow bracket notationon any object public string this[int a, double b] { … } Related to C++ operator[ ] overloading Special property
  • 6.
    public class ListBox:Control { private string[] items; public string this[int index] { get { return items[index]; } set { items[index] = value; Repaint(); } } } ListBox listBox = new ListBox(); listBox[0] = "hello"; Console.WriteLine(listBox[0]); Indexers  Lets an instance behave as a virtual array  Can be overloaded  Can be read-only, write-only, or read/write