I was looking at this question, and it made me wonder.
Whenever I define a class with auto properties,
// Example A
public class MyObject
{
public int MyInt { get; set; }
}
the JIT compiler will convert it to similar to this:
// Example B
public class MyObject
{
private int _MyInt;
public int get_MyInt()
{
return _MyInt;
}
public void set_MyInt(int value)
{
_MyInt = value;
}
}
So you could hypothetically write something like the following:
// Example C.1
public class MyObject
{
public int MyInt { get; set; }
public void set_MyInt(string value)
{
MyInt = int.Parse(value);
}
}
Or something potentially like this:
// Example C.2
public class MyObject
{
private int _myInt;
public int MyInt
{
get { return _myInt; }
set
{
_myInt = value;
}
set(string)
{
_myInt = int.Parse(value);
}
}
}
And have this functionality exist without compiler errors.
// Example D
public void DoSomething(string someIntegerAsAString)
{
var myObject = new MyObject()
{
MyInt = someIntegerAsAString
};
}
What's stopping the compiler from saying code such as Example D, where the desired result is inferred and it works correctly and expected? The functionality is there, shown in Example B.
Is this something that's against how the language designers have designed the language to work and behave?