Scott Hanselman

Hanselminutes Podcast 200 - Jon Skeet!

February 16, '10 Comments [3] Posted in Podcast
Sponsored By

jonskeet My TWO-HUNDREDTH podcast is up. It's show 200, and we've got Jon Skeet. Jon writes Java at Google, but he's also got a new book out called C# in Depth, Second Edition. Jon is also well-known for his answers on StackOverflow, gaining him the title "The Chuck Norris of Programmers." Listeners can get 40% off with code "HanselC40" at http://manning.com/skeet2 until March 15th. Someone even made an Ask Jon Skeet search engine!

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Download: MP3 Full Show

Links from the Show

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

I want to add a big thanks to Telerik. Without their support, there wouldn't be a Hanselminutes. I hope they, and you, know that. Someone's gotta pay the bandwidth. Thanks also to Carl Franklin for all his support over these last 4 years!

Telerik is our sponsor for this show.

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface and developer tools, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET AJAX,MVC,Silverlight, Windows Forms and WPF. Enjoy developer tools like .NET reporting, ORM, Automated Testing Tools, TFS, and Content Management Solution. And now you can increase your productivity with JustCode, Telerik’s new productivity tool for code analysis and refactoring. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Back to Basics: C# 4 method overloading and dynamic types

February 12, '10 Comments [30] Posted in Back to Basics | Learning .NET | Programming | VS2010
Sponsored By

C# 3.0 introduced the implicit type "var". I've explained var as saying:

"I'm too lazy to tell you the type of this variable, so you figure it out, compiler."

However, it's more useful than just promoting terseness or laziness:

var i = 10; // implicitly typed
int i = 10; //explicitly typed

Although var is a great way to start arguments about coding standards at your work, there are times when it is required around anonymous types. Here's an example from MSDN:

/ Example: var is required because
// the select clause specifies an anonymous type
var custQuery = from cust in customers
where cust.City == "Phoenix"
select new { cust.Name, cust.Phone };

// var must be used because each item
// in the sequence is an anonymous type
foreach (var item in custQuery)
{
Console.WriteLine("Name={0}, Phone={1}", item.Name, item.Phone);
}

C# 4 (not 4.0, the marketing folks say it's .NET 4, etc.) adds the dynamic keyword. I've explained this saying:

"There's no way for you or I to know the type of this now, compiler, so let's hope that the runtime figures it out."

Here's how this looks from an Intellisense point of view. Here I'm hovering over the dynamic keyword:

dynamiccalc1

And here is the tooltip after pressing "." after "calc."

dynamiccalc2

Now, to the interesting question of the day. Christoff Turner asked this question, essentially:

"I noticed the following while doing some research within C# 4.0:"

using System;
namespace ConsoleApplication1
{
class Program
{
static void f(Int32 x) { }
static void f(dynamic x) {}
static void f(Int32 x, dynamic y) {}
static void f(dynamic x, Int32 y) {}
static void f(Int32 x, dynamic y, Int32 z) {}
static void f(dynamic x, Int32 y, dynamic z) {}
static void Main(string[] args)
{
f(10); // Works - obvious
f(10, 10); // Ambiguous - obvious
f(10, 10, 10); // Ambiguous - not so obvious - since it should be possible to resolve
}
}
}

"Looking at f(10,10,10), what is the reasoning behind this call being ambiguous?"

I stared it it a while longer, then realized what is happening, and called Mads Torgersen to confirm. Mads says this.

In short, the behavior is totally by design:

  • dynamic in method signatures doesn’t come into it: it behaves like System.Object does.
  • Given that, neither of the ternary signatures is better because each fits better than the other on some arguments (Int32 fits 10 better than object does)

The key point here, in bold, because it's significant is: having the type dynamic means “use my runtime type for binding”.

The problem in the context of method calls is that you can't use the runtime type of something until, um, runtime. ;) Binding with dynamic expressions (dynamic binding) happens at runtime.  In this case, we're compiling against method signatures that are known at compile type and we're compiling with a (mostly) static language, so there's no dynamic method dispatching happening that could select a different method overload at runtime.

The dynamic type is statically-typed as dynamic. The compile-time type is "dynamic" (but really it's object, hence the method overloading trouble.)

Another way to look at this is with Reflector. This C# code:

static void f(Int32 x, dynamic y, Int32 z) {}

is essentially this, from a method signature point of view:

static void f(int x, [Dynamic] object y, int z) {}
and if there was a method that returned dynamic, it'd look like this:
[return: Dynamic]
private static object GetCalculator() {}

So, given how dynamic works as a type, how the DLR works in the context of method dispatching, etc, you can see why things didn't work like Christoff thought they would.

Enjoy!

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

The Weekly Source Code 49 - SmallBasic is Fun, Simple, Powerful Programming for Kids and Adults

February 9, '10 Comments [20] Posted in Learning .NET | MSDN | Programming | Silverlight | Source Code
Sponsored By

While the boys are only 2 and 4, I'm always keeping an eye out on new ways to teach them programming. Certainly I hope they'll be more well-rounded and I and spend more time outside, but a even a basic background in programming and logic, I think, produces a more empowered individual.

Created by Vijaye Raji, Small Basic is a simple (only 15 keywords) but powerful environment for getting started programming. Great for kids and non-technical spouses, but powerful enough even for the professional game developer. In fact, Small Basic is probably the fastest and simplest way I've seen yet to produce and publish Silverlight-based games. Read on to see why.

Small Basic is part of MSDN DevLabs and just released version 0.8. It's the eighth installment, but I suspect they are too modest to call it 8.0. ;) It's even internationalized in English, Chinese, French, German, Italian, Japanese, Jorean, Russian, Spanish, Brazilian Portuguese and Turkish...so if you know a computer teacher, you might tell them about this!

Take a look at Small Basic Tetris, for example. You can run it in the browser with Silverlight, right here. The full Small Basic source code for the app is listed right on the page. There's a bunch of great sample Small Basic apps here also.

It has a nice friendly IDE (Integrated Development Environment) with a clever take on Intellisense as seen below. The IDE goes out of its way to give you as much information and context as possible not only with the intellisense "arc" but also context-sensitive help in the right doc.

The Small Basic IDE

Even more clever, I think, is the "Graduate" button that will convert your Small Basic program into Visual Basic for use directly in Visual Studio.

Here's what a Small Basic text mode application would look like:

number = 100
While (number > 1)
TextWindow.WriteLine(number)
number = number / 2
EndWhile

Here's a SmallBasic Windows app:

GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.Width = 200
GraphicsWindow.Height = 160
GraphicsWindow.PenColor = "Blue"
For i = 1 To 10
GraphicsWindow.PenWidth = i
GraphicsWindow.DrawLine(20, i * 15, 180, i * 15)
EndFor

For those of us old enough to remember learning to program with LOGO and its ubiquitous Turtle, Small Basic includes a Turtle object built in! Remember this?

sides = 50
length = 400 / sides
angle = 360 / sides
Turtle.Speed = 9
For j = 1 To 20
For i = 1 To sides
Turtle.Move(length)
Turtle.Turn(angle)
EndFor
Turtle.Turn(18)
EndFor

Which gives us this image. See the turtle inside?

The LOGO Turtle drawing Circles in SmallBasic

The most amazing part from a learning perspective is the ability to publish your game directly to the Small Basic website and play it or give it to your friends. I hit Publish for this Turtle app and Small Basic - in one click - gave me this link: http://smallbasic.com/program/?WKN265. Now I can send my friends or students to that link.  They can play the game LIVE, see the source listing right there, or even embed it on their own website with included HTML.

I'm really impressed with the amount of though that was put into this app and how easy it was. I hope other folks at Microsoft check it out and appreciate the simplicity.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

SmallestDotNet Update - Now with .NET 4 support and an includable JavaScript API

February 9, '10 Comments [15] Posted in ASP.NET | ASP.NET MVC | Javascript | Learning .NET | Microsoft | Source Code | Tools | VS2010 | Windows Client | WPF
Sponsored By

A few years back I wrote a post on the size of the .NET Framework. There's historically been a lot of confusion on the site of the .NET Framework. If you search around on the web for ".NET Framework" or ".NET Framework Redistributable" you'll often get a link to a 200 meg download. That download is the complete offline thing that developers redistribute when they want to install the .NET Framework on any kind of machine without an internet connection.

The .NET 3.5 Client Profile is more like 28 megs and the .NET 4 Client Profile is a looking smaller than that, in fact. Back then I made this website,SmallestDotNet.com to help out. It'll sniff your browser's UserAgent and tell you want version of .NET you have, how big the download would be to get you to .NET 3.5 and what .NET redistributable is best for you in order to minimize your download.

Now that the .NET Framework 4 is coming out soon, I took an hour and updated it (with Tatham Oddie's help, as he's staying over at the house this week) to support the new framework. We also added a few bug fixes (and I'm sure, a few bugs) and a simple Javascript API to help you detect the .NET Framework on the client side.

You can use the site in three ways.

First, visit the site.

If you've got a machine you want to install .NET on, or you're not sure what version it has, just visit SmallestDotNet.com and I'll do my best to tell you. It works best on IE, but it'll also work on Firefox if you've got the .NET add-on. If you've got Safari or Chrome, I'll apologize but I can't help you as those browsers don't tell me anything about .NET.

Second, include the Javascript that spits HTML.

If you want to tell the user what version of .NET they have with minimal effort and you want to do it on your site, perhaps via your blog, you can just include this line:

<script type="text/javascript" src="http://www.smallestdotnet.com/smallestdotnet/javascript.ashx"></script>

and I'll return something like:

document.write('<span class="smallerdotnet">')
document.write('Detected 3.5 SP1 .NET Framework. No update needed.')
document.write('</span>')

and you can style to taste.

Third, include the Javascript that spits JSON.

The HTML spitter is fast, but less useful if you like control over things. Like, um, text. So, you can include:

<script type="text/javascript" src="http://www.smallestdotnet.com/smallestdotnet/javascriptdom.ashx"></script>

and I'll spit out a JSON object like this:

SmallestDotNet = {};
SmallestDotNet.latestVersion = {
major: 4,
minor: 0,
profile: "client",
servicePack: null
};
SmallestDotNet.allVersions = [
{
major: 4,
minor: 0,
profile: "client",
servicePack: null
},
{
major: 3,
minor: 5,
profile: "full",
servicePack: 1
},
{
major: 2,
minor: 0,
profile: "full",
servicePack: null
}
];

We'll put out the latest version of the .NET framework on the machine, as well as its Service Packs, and its profile (client profile, full, etc) if appropriate. It'll also give you an array of ALL the versions of the .NET Framework it finds on the machine. In my example, it says my machine has .NET 4 Client Profile, .NET 3.5 SP1 and .NET 2.0.

UPDATE: I've updated the JSON output to return another array with a complete list of all available .NET Frameworks and the URL they can be downloaded from, something like this:

SmallestDotNet.downloadableVersions =
[{
major: 4,
minor: 0,
profile: 'client',
servicePack: null,
url: 'http://www.microsoft.com/...'
},
...SNIP...

{
major: 2,
minor: 0,
profile: 'full',
servicePack: 2,
url: 'http://www.microsoft.com/...'
},{
major: 1,
minor: 1,
profile: 'full',
servicePack: 1,
url: 'http://www.microsoft.com/...'
}];

I currently don't go into deep deep detail, like .NET 2.0 SP2, etc, but if you want that functionality, let me know and I can add it. This is a spike (ongoing for two years, actually) so if it's useful, let me know. If it's missing something, let me know.

From this JSON, you can ask all sorts of questions. Here's a JavaScript alert() example that would work on this JSON object. Of course, you should check things for null, as well as check the length of allVersions.

alert( SmallestDotNet.latestVersion.major ); 
alert( SmallestDotNet.allVersions.length );
alert( SmallestDotNet.allVersions[0].minor );
alert( SmallestDotNet.allVersions[1].major );");

It'd be cool to add some jQuery love to this and use it to give and end-user some nice feedback on what version of your application to install, or what version of .NET they should install first. It could also be useful for self-troubleshooting your application.

Hope this is useful. Enjoy.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Hanselminutes Podcast 199 - How Craigslist Works - with Jeremy Zawodny

February 6, '10 Comments [2] Posted in Open Source | Podcast | Source Code
Sponsored By

Jeremy Zawodny My one-hundred-and-ninety-ninth podcast is up. I chat with Jeremy Zawodny, a developer at Craigslist on how the system is put together. How many servers do they have? How does it all fit together and what are the major technology problems they have to solve?

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Download: MP3 Full Show

Links from the Show

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show.

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface and developer tools, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET AJAX,MVC, Silverlight, Windows Forms and WPF. Enjoy developer tools like .NET reporting, ORM, Automated Testing Tools, TFS, and Content Management Solution. And now you can increase your productivity with JustCode, Telerik’s new productivity tool for code analysis and refactoring. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.