Generic Programming in C#

Posted by Filip Ekberg on July 25 2011 5 Comments

This is my fourth screencast, talking about generic programming in C#. In this session I talk about how life was before we had Generics and then I talk about what generics solves for us.

If you have any questions please leave me a comment.

Vote on HN

C# 4.0 Using the Dynamic Keyword

Posted by Filip Ekberg on July 21 2011 Leave a Comment

This is my third screencast, talking about the dynamic keyword in C# 4.0. In this session I talk briefly about the dynamic keyword, where you might have seen it before and give some examples on how you can use it. I won’t go in to depth on the DRL, ExpandoObject or when/when not to use the Dynamic Keyword, that’s a whole other video.

As I stated in my last post I want to do short 8 – 15 minute videos scratching on the surface and then go in depth on the parts that most of you find interesting!

If you have any requests or questions regarding this or any other of my videos, don’t hesitate to leave a comment.

Vote on HN

More C# screencasts on the way!

Posted by Filip Ekberg on July 11 2011 Leave a Comment

It seems like the videos on C# 5 and the Async CTP were appreciated so I am working on some more screencasts. They will be a bit different than the Async CTP videos though.

The major difference will be that they will be shorter, I will try not to break 10-15 minutes in order to make the videos as effective and fun possible. On the other hand I will instead make more videos on each subject.

To start this off, you can expect to see videos regarding Generics and Dynamic programming in C#, each will most likely be divided as followed:

  • A brief overview and where we might have seen it been used before
  • Refactor your old code to adapt it and make it more maintainable
  • Deep dive

If the cast is too short or the above won’t work on each subject, I’ll add some extra candy to the mix! As I am only doing 10-15 minutes on each video, I might not cover every corner, in case I miss something I can do a follow up video or post here, just give me a comment and we’ll figure something out.

If you got any suggestions for what you might want to see, give me a ping on twitter or leave a comment.

Vote on HN

WPF vs WinForms – Which is easier to learn?

Posted by Filip Ekberg on July 10 2011 3 Comments

This question originates from StackOverflow, but as I see there are a lot of beginners that ask this question, I wanted to share the answer on my blog as well.

So, WPF vs WinForms – Which is easier to learn?

Neither and Both, easy is very subjective and depends on your background. First of all, I want to show an example of an application with the following in both WPF and WinForms:

  • A textbox for input
  • A button to do some “Processing” of the input
  • A label that will display a result

WinForms

The window can look something like this:

enter image description here

The code for this is quite trivial and easy even for a beginner:

private Button _myButton;
private Label _resultLabel;
private TextBox _inputTextBox;
public Form1()
{
    InitializeComponent();
    _myButton = new Button
    {
        Text = @"Process data",
        Top = 100
    };

    _inputTextBox = new TextBox();

    _resultLabel = new Label
    {
        Text = "",
        Top = 200
    };

    Controls.Add(_myButton);
    Controls.Add(_inputTextBox);
    Controls.Add(_resultLabel);

    _myButton.Click += Process_Click;    
}

void Process_Click(object sender, EventArgs e)
{
    _resultLabel.Text = string.Format("Your data is: {0}", _inputTextBox.Text);
}

So far, we haven’t touched anything in the designer.

WPF

If we copy and paste the exact same code into a new WPF-application, there are just a handfull of things that we have to change some of them being:

  • How we add the controls to the interface
  • How we change text in a label
  • How we position it from the top ( margin )

However, these are just things that is Different between the two, it’s neither more difficult nor more easy in one or the other. So this is what the window looks like in WPF:

enter image description here

And the code is Almost identical:

private Button _myButton;
private Label _resultLabel;
private TextBox _inputTextBox;
public MainWindow()
{
    InitializeComponent();

    _myButton = new Button
    {
        Content = @"Process data",
        Margin = new Thickness(0, 100, 0, 0)
    };
    _myButton.Click += Process_Click;

    _inputTextBox = new TextBox();
    _resultLabel = new Label
    {
        Content = "",
        Margin = new Thickness(0, 100, 0, 0)
    };
    var panel = new StackPanel();
    panel.Children.Add(_inputTextBox);
    panel.Children.Add(_myButton);
    panel.Children.Add(_resultLabel);  
    Content = panel;
}

void Process_Click(object sender, EventArgs e)
{
    _resultLabel.Content =
                 string.Format("Your data is: {0}", _inputTextBox.Text);
}

So far both WPF and WinForms seem to be equivilient hard / easy to learn. But this is just the code-behind approach, when we come to the designer, there’s a huge difference.

WinForms

When you develop WinForms applications you drag and drop controls onto your surface and use a property window to change the properties of your window. This looks something like this:

enter image description here

So you can drag-n-drop your controls, you can change the properties of each control and snap them to where you want them, easy enough right?

WPF

So what about WPF? Is it much harder to do the same thing there? I think not:

enter image description here

The main difference here is that you have an extra “window” at the bottom that. This window is an XML ( XAML ) view of your design. This is where WinForms and WPF take apart from each other. But just as you can avoid writing Design Code in WinForms, you can Avoid doing it in WPF as well, as a newbie that is.

As for newbies, I don’t think it’s harder nor easier to learn the one or the other, when we get a bit deeper into the technology that is choosen, sure, it gets more complex. But the way there is just as easy or hard no matter which of the two you choose.

So when do you choose the one or the other?

You choose WinForms because:

  • It’s been around for a long time and you have a Large control supply that you can use.
  • There are a lot of good resources on WinForms to learn from and to get new controls from.

You choose WPF because:

  • You can make richer UI, nowdays it’s all about the user experience.
  • You want to have full control of your controls design.
  • You want rich, data-driven applications.
  • You want hardware accelerated UI.

How about data-binding, Design Patters and all that?

You don’t need to know anything about Design Patterns nor how everything works to make a usabel application, especially not as a newbie. As time goes by, you will have to learn more about the technology that you choose. People tend to say that you need to know MVVM to use WPF, that is not true. As it is not true that you need to know the MVP pattern to create WinForms applications.

Both of the technologies can handle rich-data-driven controls, you have grid views and such in both of them. WinForms has some pretty nice “drag-n-drop”-features for data manipulation and listing and WPF has very nice data-binding.

What if I don’t want to choose one or the other?

The beauty is that you don’t have to! You can host WinForms controls in WPF and you can host WPF controls inside WinForms. This means that if you are developing a WinForms application and you want to take advantages of WPF, that’s ok you can!

So which is easier?

Neither and Both! As for a newbie, both can look a lot similar on the surface, even though it is so different when you go deeper.

They are similar but so different, the thing is that when you start out, you have other things to think about than how the Routing model works and how to adapt to MVVM.

Is XAML dead?

No. XAML isn’t dead. WP7 uses Silverlight which is also using XAML. Even though a lot of development in the future can be done with HTML5, I doubt that XAML is about to “die”. People asked if WinForms was going to die when WPF was released and it hasn’t.

 

Vote on HN