I see you mention ‘objective-c’ and the use of the ‘new’ keyword in that language. Those coming from C++ might also be thrown by the C# use of the ‘new’ keyword. The ‘new’ keyword is only used when you’re dealing with a pointer.
In C# being a class/struct is all that controls this. You don’t need to define pointers. All classes are always referenced, and structs are always treated as values (unless you explicitly ‘ref’ or ‘out’ them as parameters… or use pointers when scoped in an ‘unsafe’ block of code).
This means that ‘new’ really doesn’t serve any purpose anymore in regards to actual memory allocation. It’s just implied code. For all intents and purposes… we could throw it out.
BUT
How about naming? C++ and objective-c are plagued with weird naming systems to get around all the weird name collisions that can occur. This results in communal standards to naming stuff, but those standards can be very localized, and there’s really no controlling it… you screw up some naming mechanic, and as long as there are no collisions, the compiler doesn’t care. The name of that class, and that field, are just gibberish lines of characters to the compiler.
Of course over the years the various C/C++/objective-C standards have come up with systems to try and remedy this. But these too were often bandaids, and weren’t always included in every compiler. And even so, developers usually just stuck to habit.
Where as with .Net/C#, Microsoft had 100% control, they get to create standards that stick. And they get to them before the community molests them.
So lets consider the situation of a class with a property that has the same name as another class that exists:
public struct Data
{
public int Id;
public int Value;
public Data(int id, int val)
{
Id = id;
Value = val;
}
}
public struct Foo
{
public Data Data;
public void SetData(int id, int val)
{
Data = new Data(id, val);
}
}
This is legit code. It works. It can determine what the difference between the various uses of ‘Data’. Because the act of setting Data to a new Data uses the ‘new’ keyword. It syntactically can work out.
Where as in C++:
struct Data
{
int Id;
int Value;
Data() {};
Data(int id, int val)
{
Id = id;
Value = val;
}
};
struct Foo
{
Data Data;
void SetData(int id, int val)
{
Data = Data(id, val);
}
};
This fails, the compiler doesn’t know the difference between the various uses of ‘Data’ in the code.
Another situation might be where you happen to have a method with the same name. Now it’s even MORE shaped like the constructor. C# just basically decided that ‘new’ is used to denote the use of the constructor function for a type.
One might argue that the field ‘Data Data’ is a bad name. But why? What should I call it? Should I change the struct name to be ‘DataInfo’ and the field can remain ‘Data’?
In the end… it’s just the way C# do.