tags:

views:

201

answers:

4

So for complex types, we can write:

return new MyType ( 5 );

but why can't we do stuff like (to have symmetry for one):

return new int ( 5 );

return new Int64 ( 5 );

I know only parameterless constructors are provided. What's the reason for this?

A: 

This may sound odd but why would you want to? - complex types make sense since you are using 1-n inputs to get a result but the int just returns an int...so why?

Tab
Was just wondering out of curiosity. Like you might just wanna use types like Int64 directly, like you would other types, even if the type might convert from other types like: MyType mine = 5;
Joan Venge
I see, I did not read all the comments but I have a great interest in extension methods and DSL's which might enable the type of behavior you are curious about. Interesting leg up on extension methods: http://weblogs.asp.net/dwahlin/archive/2008/01/25/c-3-0-features-extension-methods.aspx
Tab
+11  A: 

What would be the point? Any of the .NET value types that don't have a constructor have a literal form. If they didn't have a literal form, it would be impossible to provide a constructor that takes its type as a parameter.

Since you have the literal, providing a constructor which takes that literal makes no sense.

Without literal, impossible
int i = new int(new int(new int(...)));
With literal
int i = new int(5); // What is the point? Would the constructor do *anything*
                    // besides int i = 5?
Samuel
+2  A: 

Part of design is knowing what to leave out. Since it doesn't add value, it wasn't coded.

Scott Weinstein
+1  A: 

It's just that these types only have a default constructor. A lot of other complex .net type only have a default constructor as well. I would guess the reason that there isn't a parameterized one is because, as mentioned by other answers, there isn't really a good reason for one.

Jacob Adams