This[r, c] saw this in some C# code, how is it possible?

  • C#
  • Thread starter NotASmurf
  • Start date
  • Tags
    Code
In summary, in C#, a variable can have multiple indices by being declared as a multidimensional array, using the first index to represent rows and the second index to represent columns. Square brackets are used to indicate an array and the number inside represents the size of the array. Specific elements in a multidimensional array can be accessed using the variable name followed by the index values in square brackets. It is possible for a variable to have more than two indices, known as a jagged array, allowing for more complex data structures. There is no specific limit to the number of indices a variable can have, but it is important to consider memory and performance implications.
  • #1
NotASmurf
150
2
Hey all, I was looking at some example code where they said "this[r, c] = sourceMatrix[r, c];"
where sourcematrix is double[][] but its in a custom local class, the code runs and works. but I am confused as to what "this[r, c]" does, how can a custom class be an array of doubles at the same time?? Any help appreciated.
 
Technology news on Phys.org
  • #2
Sorry nevermind, seems you can overload c# keywords now.
Code:
 public double this[int row, int col]
        {
            get
            {
                return this.matrix[row, col];
            }
            set
            {
                this.matrix[row, col] = value;
            }
        }
 
  • Like
Likes harborsparrow and WhatHitMe

Related to This[r, c] saw this in some C# code, how is it possible?

1. How is it possible for a variable to have multiple indices in C#?

In some cases, a variable in C# can be declared as a multidimensional array, meaning it can have more than one index. This allows for storing data in a table-like structure, where the first index represents the row and the second index represents the column.

2. What is the purpose of using square brackets in a variable declaration?

Square brackets are used in C# to indicate that a variable is an array, meaning it can hold multiple values. The number inside the brackets represents the size of the array, or the number of elements it can hold.

3. How do you access a specific element in a multidimensional array in C#?

To access a specific element in a multidimensional array in C#, you can use the variable name followed by the index values in square brackets. For example, if the variable name is "myArray", and you want to access the element at row 2, column 3, you would use myArray[2, 3].

4. Can a variable in C# have more than two indices?

Yes, it is possible for a variable in C# to have more than two indices. This is known as a jagged array, where each element in the array can be another array with its own set of indices. This allows for more complex data structures to be created and accessed.

5. Is there a limit to the number of indices a variable can have in C#?

There is no specific limit to the number of indices a variable can have in C#. However, it is important to consider the memory and performance implications of using a large number of indices, as it can impact the efficiency of your code.

Similar threads

  • Programming and Computer Science
Replies
1
Views
981
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
1
Views
792
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
406
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
8
Views
918
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
6
Views
962
Back
Top