I am only a beginner to C#, but am trying to learn everything. However, I am stuck on the overloading true and false; what does this mean? Please (try to) give replies as basic as possible (so that even a '13' yr old understands the logic). (Please make to sure make it as understandable as possible, thank you). If you can, please explain what the outcome would be if the coord had changed to something else (such as: (3,5)).
I have pre-made code right here: (Can you lease explain the different outputs on changing the coordinates) Info: - Program is a console application - C# I have made a class called Coord (standing for co-ordinates). Inside Class:
class Coord
{
private int _x, _y;
public Coord(int x, int y)
{
_x = x;
_y = y;
}
public int x
{
get
{
return _x;
}
set
{
_x = value;
}
}
public int y
{
get
{
return _y;
}
set
{
_y = value;
}
}
public static bool operator true(Coord coord1)
{
if (coord1.x != 0 || coord1.y != 0)
{
return true;
}
else
{
return false;
}
}
public static bool operator false(Coord coord1)
{
if (coord1.x == 0 && coord1.y == 0)
{
return true;
}
else
{
return false;
}
}
}
Inside Main class Program:
class Program
{
static void Main(string[] args)
{
Coord coord = new Coord(0, 0);
if (coord)
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
Console.ReadLine();
}
}
I am only a beginner to C#, but am trying to learn everything.
Make sure to allocate a couple of decades minimum. I learn new things about this language quite frequently.
I am stuck on the overloading true and false; what does this mean?
They are seldom used; they are primarily needed to make user-defined && and || operators be short-circuiting.
Read my series of articles on this subject; you will be particularly interested in part three but you should read the whole thing.
https://ericlippert.com/2012/03/26/null-is-not-false-part-one/
If you can, please explain what the outcome would be if the coord had changed to something else (such as: (3,5)).
If you want to know what a program does when you change something, change it and run it and soon you will know!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With