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

  • Thread starter Thread starter NotASmurf
  • Start date Start date
  • Tags Tags
    Code
Click For Summary
The discussion revolves around the use of indexers in C#, specifically how a custom class can behave like an array. The code snippet demonstrates an indexer defined in a class that allows access to a two-dimensional array of doubles, referred to as `matrix`. The syntax "this[r, c]" is used to get or set values in the `matrix` using row and column indices, effectively overloading the array access functionality. This feature enables the custom class to mimic array behavior, allowing for intuitive data manipulation while maintaining encapsulation of the underlying data structure. The confusion initially expressed is clarified by understanding that C# allows the creation of indexers, which can enhance the usability of custom classes.
NotASmurf
Messages
150
Reaction score
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
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
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K