Have you ever wished if there could be a type that could be converted at least to all primitive types?
- One type to handle strings.
- One type to handle numerics.
- One type to handle date & time.
- One type to convert them all.
I don't know if this is a good behaviour in clean designs or not, but it was interesting to me to do so. In addition, I encountered using dynamic types in many languages, especially in scripting and string processing languages like Python.
I heard about integrating something like dynamic typing in C# 3.0. I think it is a bad news. Because C# will be no more a type-safe programming language. Microsoft plans to import everything to it, and I think that will be a buzz oriented language and will produce product oriented developers. After all, I wrote this. You may use it in scenarios like propagating values to UI.
All of the primitive types - which implements the
IConvertible interface - can be converted to this type and vice versa.
Using the code
Using the code is simple. You can include the two files [available in the download file above] in your project and compile them in the whole project.
Notice that there are special situations where the C# compiler can not retrieve the type of the variable. In such cases, just cast the variable to the target type. Here is a small sample code:
Variable v = 100;
string s = v;
s = "10.8";
v = s;
double d = v;
Interesting huh? Just try it and let me know how it was useful (if it happens :) ).
There is a long history about dynamic typing and static types vs. dynamic types. But I think these are different areas and respond to solving different problems. Integrating all of the possible features that are used in various languages and solutions will lead us to spaghetti code again. And this time, at a higher level, even in conceptual levels.
A .NET Developer came from a 'C' and 'C++' background with some experiences in low level programming. In last 2 years I have performed various works in ASP.NET 2.0, .NET compact framework and other .NET things.
I am looking forward for functional programming to be mainstream (Lisp (Scheme), F#, Erlang and Haskell (Clean)). There are two path there I love them both: Lisp path (Power of macros with Scheme, ...) and ML path(Power of ADT and patterns with F#, Haskell, ...) and there are some middle ground too (Nemerle, Scala).