Skip to content

Instantly share code, notes, and snippets.

@GSoster

GSoster/comparison.cs

Last active Jan 14, 2019
Embed
What would you like to do?
dotnetOperatorOverloading1
public static bool operator > (Point a, Point b)
{
return a.X > b.X;
}
public static bool operator < (Point a, Point b)
{
return a.X < b.X;
}
using System;
public class Point{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public static Point operator+ (Point a, Point b)
{
int x = a.X + b.X;
int y = a.Y + b.Y;
return new Point(x, y);
}
public static bool operator > (Point a, Point b)
{
return a.X > b.X;
}
public static bool operator < (Point a, Point b)
{
return a.X < b.X;
}
}
using System;
public class Point{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public static Point operator+ (Point a, Point b)
{
int x = a.X + b.X;
int y = a.Y + b.Y;
return new Point(x, y);
}
}
using System;
public class Point{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
}
using System;
namespace OperatorOverloading
{
class Program
{
static void Main(string[] args)
{
Point a = new Point(10, 2);
Point b = new Point(2, 10);
Point c = a + b;
Console.WriteLine($"Point C.X = {c.X} C.Y = {c.Y}"); // prints: Point C.X = 12 C.Y = 12
}
}
}
using System;
namespace OperatorOverloading
{
class Program
{
static void Main(string[] args)
{
Point a = new Point(10, 2);
Point b = new Point(2, 10);
Point c = a + b;
//Print: Point C.X = 12 C.Y = 12
Console.WriteLine($"Point C.X = {c.X} C.Y = {c.Y}");
//Print: Is a.X (10) greater than b.X (2)? True
Console.WriteLine($"Is a.X ({a.X}) greater than b.X ({b.X})? {a > b}");
//Print: Is a.X (10) less than b.X (2)? False
Console.WriteLine($"Is a.X ({a.X}) less than b.X ({b.X})? {a < b}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.