ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

An experiment about static and dynamic type systems

<< < (2/2)

Renegade:
C# allows dynamic typing, but I'll be damned if I'll use it if I can at all avoid it. (dynamic and var)-Renegade (September 12, 2011, 11:21 PM)
--- End quote ---
'var' is not dynamic typing, it's used for static type inference which is super useful for DRY reasons.

'dynamic' is something to be very careful about - it tends to ripple out if you start using it.
-f0dder (September 13, 2011, 01:54 PM)
--- End quote ---

I stand corrected. :)

But close enough. When you look at it, var looks like a dynamic type. e.g. var acme = "Rocket Skates"; vs. var acyou = 500;

http://msdn.microsoft.com/en-us/library/bb383973.aspx

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:
--- End quote ---

http://msdn.microsoft.com/en-us/library/bb384061.aspx

Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.
--- End quote ---

However, this:


--- Code: C# ---var a = 1;var b = "123";var c = a + b;MessageBox.Show(c.ToString());
Outputs:

1123

And


--- Code: C# ---var a = 1;var d = a + 2;var b = "123";var c = d + b;MessageBox.Show(c.ToString());
Outputs:

3123

Which really makes it *look like* a dynamic type.

I've noticed that with dynamic -- it tends to be infectious. I've found it's useful for dealing with JSON and all that gooey webishness. :)

f0dder:
C# allows dynamic typing, but I'll be damned if I'll use it if I can at all avoid it. (dynamic and var)-Renegade (September 12, 2011, 11:21 PM)
--- End quote ---
'var' is not dynamic typing, it's used for static type inference which is super useful for DRY reasons.

'dynamic' is something to be very careful about - it tends to ripple out if you start using it.
-f0dder (September 13, 2011, 01:54 PM)
--- End quote ---
I stand corrected. :)

But close enough. When you look at it, var looks like a dynamic type. e.g. var acme = "Rocket Skates"; vs. var acyou = 500;-Renegade (September 13, 2011, 05:50 PM)
--- End quote ---
Sure, it does look like dynamic types at first, but the distinction is super-important. I personally loathe dynamic typing, but I'm a big fan of type inference, as it makes my life easier without giving up static typed goodness.

I'm not using it everywhere, though - I don't use 'var' when dealing with simple built-in types, and whether to use it when assigning a variable to a method return value isn't always clear-cut either. The goal to strive for is reducing unnecessary clutter, while not adding ambiguity; remove noise so you can focus on the important parts.

Your strings and numbers" samples show that implicit type conversions and operator overloading isn't always a good idea - and, especially since C# has such nice string formatting, it int-to-string conversion should IMHO have been explicit.

I've noticed that with dynamic -- it tends to be infectious. I've found it's useful for dealing with JSON and all that gooey webishness. :)-Renegade (September 13, 2011, 05:50 PM)
--- End quote ---
Can't get by just with anonymous types? :)

Navigation

[0] Message Index

[*] Previous page

Go to full version