본문 바로가기

Software/C#.Net

Indexer Methods

Overloading DataTable objects indexer
public class SomeContainer 
{ 
  private int[,] my2DintArray = new int[10, 10];
 
  public int this[int row, int column] 
  {  /* get or set value from 2D array */  } 
} 

Usage: In ADO.NET, DataTable has columns and rows, let create a table with three columns. and try to access by indexer
static void MultiIndexerWithDataTable() 
{ 
  // Make a simple DataTable with 3 columns. 
  DataTable myTable = new DataTable(); 
  myTable.Columns.Add(new DataColumn("FirstName")); 
  myTable.Columns.Add(new DataColumn("LastName")); 
  myTable.Columns.Add(new DataColumn("Age")); 
 
  // Now add a row to the table.  
  myTable.Rows.Add("Mel", "Appleby", 60); 
 
  // Use multi-dimension indexer to get details of first row.
  Console.WriteLine("First Name: {0}", myTable.Rows[0][0]); 
  Console.WriteLine("Last Name: {0}", myTable.Rows[0][1]); 
  Console.WriteLine("Age : {0}", myTable.Rows[0][2]); 
} 

Overloadable operator in C#

 +, -, !, ~, ++, --, true, false These unary operators can be overloaded 
 +, -, *, /, %, &, |, ^, <<,>>  These binary operators can be overloaded 
==, !=, <, >, <=, >=   These comparison operators can be overloaded. C# demands that “like” operators (i.e., < and >, <= and >=, == and !=) are overloaded together. 
 []  The [] operator cannot be overloaded. As you saw earlier in this chapter, however, the indexer construct provides the same functionality 
 () The () operator cannot be overloaded. As you will see later in this chapter, however, custom conversion methods provide the same functionality 
 +=, -=, *=, /=, %=, &=, |=,^=, <<=, >>= Shorthand assignment operators cannot be overloaded;however, you receive them as a freebie when you overload the related binary operator 
 
Oeverloading Unary Operator
public class Point 
{ 
... 
  public static Point operator + (Point p1, int change) 
  { 
    return new Point(p1.X + change, p1.Y + change); 
  } 
 
  public static Point operator + (int change, Point p1) 
  { 
    return new Point(p1.X + change, p1.Y + change); 
  } 

You have to make both functions What of the += and –+ Operators? In terms of C#, the shorthand assignment operators are automatically simulated if a type overloads the related binary operator. Thus, given that the Point structure has already overloaded the + and - operators, you can write the following:
// Overloading binary operators results in a freebie shorthand operator. 
static void Main(string[] args) 
{ 
... 
  // Freebie += 
  Point ptThree = new Point(90, 5); 
  Console.WriteLine("ptThree = {0}", ptThree); 
  Console.WriteLine("ptThree += ptTwo: {0}", ptThree += ptTwo); 
 
  // Freebie -= 
  Point ptFour = new Point(0, 500); 
  Console.WriteLine("ptFour = {0}", ptFour); 
  Console.WriteLine("ptFour -= ptThree: {0}", ptFour -= ptThree); 
  Console.ReadLine(); 
} 
Operator overload
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OverloadedOps
{
    public class Point : IComparable
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Point(int xPos, int yPos)
        {
            X = xPos;
            Y = yPos;
        }

        public override string ToString()
        {
            return string.Format("[{0}, {1}]", this.X, this.Y);
        }

        #region Binary ops
        // overloaded operator +
        public static Point operator +(Point p1, Point p2)
        { return new Point(p1.X + p2.X, p1.Y + p2.Y); }

        // overloaded operator -
        public static Point operator -(Point p1, Point p2)
        { return new Point(p1.X - p2.X, p1.Y - p2.Y); }

        public static Point operator +(Point p1, int change)
        {
            return new Point(p1.X + change, p1.Y + change);
        }

        public static Point operator +(int change, Point p1)
        {
            return new Point(p1.X + change, p1.Y + change);
        }
        #endregion

        #region Unary ops
        // Add 1 to the X/Y values incoming Point.
        public static Point operator ++(Point p1)
        { return new Point(p1.X + 1, p1.Y + 1); }

        // Subtract 1 from the X/Y values incoming Point.
        public static Point operator --(Point p1)
        { return new Point(p1.X - 1, p1.Y - 1); }
        #endregion

        #region Equality logic
        public override bool Equals(object o)
        {
            return o.ToString() == this.ToString();
        }

        public override int GetHashCode()
        { return this.ToString().GetHashCode(); }

        // Now let's overload the == and != operators.
        public static bool operator ==(Point p1, Point p2)
        { return p1.Equals(p2); }

        public static bool operator !=(Point p1, Point p2)
        { return !p1.Equals(p2); }
        #endregion

        #region Compare ops
        public int CompareTo(object obj)
        {
            if (obj is Point)
            {
                Point p = (Point)obj;
                if (this.X > p.X && this.Y > p.Y)
                    return 1;
                if (this.X < p.X && this.Y < p.Y)
                    return -1;
                else
                    return 0;
            }
            else
                throw new ArgumentException();
        }

        public static bool operator <(Point p1, Point p2)
        { return (p1.CompareTo(p2) < 0); }

        public static bool operator >(Point p1, Point p2)
        { return (p1.CompareTo(p2) > 0); }

        public static bool operator <=(Point p1, Point p2)
        { return (p1.CompareTo(p2) <= 0); }

        public static bool operator >=(Point p1, Point p2)
        { return (p1.CompareTo(p2) >= 0); }

        #endregion
    }
}
//-------------------------------------------------------------------------------
//main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OverloadedOps
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Overloaded Operators *****\n");

            // Make two points.
            Point ptOne = new Point(100, 100);
            Point ptTwo = new Point(40, 40);
            Console.WriteLine("ptOne = {0}", ptOne);
            Console.WriteLine("ptTwo = {0}", ptTwo);

            // Add the points to make a bigger point?
            Console.WriteLine("ptOne + ptTwo: {0} ", ptOne + ptTwo);

            // Subtract the points to make a smaller point?
            Console.WriteLine("ptOne - ptTwo: {0} ", ptOne - ptTwo);

            // Freebie +=
            Point ptThree = new Point(90, 5);
            Console.WriteLine("ptThree = {0}", ptThree);
            Console.WriteLine("ptThree += ptTwo: {0}", ptThree += ptTwo);

            // Freebie -=
            Point ptFour = new Point(0, 500);
            Console.WriteLine("ptFour = {0}", ptFour);
            Console.WriteLine("ptFour -= ptThree: {0}", ptFour -= ptThree);

            // Applying the ++ and -- unary operators to a Point.
            Point ptFive = new Point(1, 1);
            Console.WriteLine("++ptFive = {0}", ++ptFive);  // [2, 2]
            Console.WriteLine("--ptFive = {0}", --ptFive);  // [1, 1]

            // Apply same operators as postincrement/decrement.
            Point ptSix = new Point(20, 20);
            Console.WriteLine("ptSix++ = {0}", ptSix++);  // [20, 20]
            Console.WriteLine("ptSix-- = {0}", ptSix--);  // [21, 21]

            Console.WriteLine("ptOne == ptTwo : {0}", ptOne == ptTwo);
            Console.WriteLine("ptOne != ptTwo : {0}", ptOne != ptTwo);

            Console.WriteLine("ptOne < ptTwo : {0}", ptOne < ptTwo);
            Console.WriteLine("ptOne > ptTwo : {0}", ptOne > ptTwo);

            Console.ReadLine();
        }
    }
}