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

  • Context: C# 
  • Thread starter Thread starter NotASmurf
  • Start date Start date
  • Tags Tags
    Code
Click For Summary
SUMMARY

The discussion centers on the use of indexers in C#, specifically the syntax "this[r, c]" within a custom class. The code snippet demonstrates how a custom class can behave like a two-dimensional array by implementing an indexer property. This allows the class to access and modify elements in a private double matrix using row and column indices. The ability to overload C# keywords, such as the indexer, is confirmed as a feature that enhances the functionality of custom classes.

PREREQUISITES
  • C# programming language fundamentals
  • Understanding of object-oriented programming concepts
  • Knowledge of indexers in C#
  • Familiarity with arrays and matrix manipulation
NEXT STEPS
  • Study C# indexers in detail
  • Explore object-oriented programming principles in C#
  • Learn about overloading operators and keywords in C#
  • Investigate best practices for implementing custom collections in C#
USEFUL FOR

C# developers, software engineers, and anyone interested in advanced C# features such as indexers and custom class implementations.

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   Reactions: harborsparrow and WhatHitMe

Similar threads

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