C# 3.0 Tutorials: Auto-Implemented Properties (a.k.a Automatic Properties)

Note: This is the first part of my C# 3.0 tutorial series.

Auto-implemented properties is the C# 3.0 new feature that make property-declaration more concise. It helps you to save some of your time for typing a lot of codes. Please take a look the following code to know how it looks like.

class Car
{
public string Speed { get; set; }
}

Why Auto-Implemented Properties?

Looking back the way that we have been doing for creating a property in so many projects, do you notice that you have been writing so many line codes just for creating a simple property? Look at the code below.

public class Employee {
private int _id;
private string _firstName;
private string _lastName;

public int ID{
get{
return _id;
}
}

public string FirstName {
get {
return _firstName;
}
set {
_firstName = value;
}
}

public string LastName {
get {
return _lastName;
}
set {
_lastName = value;
}
}
}

Code 1.0: Employee class that doesn’t use auto-implemented properties

This is what we used to create the property in C#. In order to create one simple property, we have to type extra four or five lines of code. So, if you have one hundred properties in different entity classes, you have to type toooo much code just for creating properties.

I think VS IDE team at Microsoft also awared of this situration. That’s why when they released Visual Studio 2005 with .NET framework 2.0, we got the better way to deal with creating properties. You can simply type “prop” and press “tab” key twice. then, VS IDE will generate the property for you. Or you can use “Encapsulate Field” from “Refactor” menu to create the property. Unfortunately, those features are available only in C# project type in VS 2005. If you are using VB.NET project, you won’t be able to use that feature.

And when Microsoft shipped .NET framework 3.0, the new feature called “Auto-Implemented Properties (a.k.a Automatic Properties)” is introduced in Framework 3. It helps you to save some of your typing time for declaring private variable, setting the value to that private variable and returning the value.

Take a look at the class that I have converted from normal class (Code 1.0) to the class with auto-implemented properties. It’s much shorter, right?

class Employee
{
public int ID{ get; private set; } // read-only
public string FirstName { get; set; }
public int LastName { get; set; }
}

Code 1.1: Employee class that uses auto-implemented properties

Conclusion

Auto-Implemented Properties is one of the most popular features of C# 3.0 and a lot of developers are really happy with that new feature. but you should know that auto-implemented properties are nothing new except it helps you to boost your productivity.

FAQs

1. Can “Auto-implemented Properties” improve the performace?

No. It is just helping you to type shorter code but it doesn’t improve the performance since the compiler will generate the same as the way what it generates for normal class that doens’t have auto-implemented properties.

2. How can I make read-only or write-only auto-implemented properties?

You can make the accessor as private. For example: If you want to make read-only property then You can put “private” in setter. You can read more about that in C# 3.0 specification.

It mentioned like below in C# 3.0 specification ~

When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field.

Because the backing field is inaccessible, it can be read and written only through the property accessors. This means that automatically implemented read-only or write-only properties do not make sense, and are disallowed. It is however possible to set the access level of each accessor differently. Thus, the effect of a read-only property with a private backing field can be mimicked like this:

public class ReadOnlyPoint {
public int X { get; private set; }
public int Y { get; private set; }
public ReadOnlyPoint(int x, int y) { X = x; Y = y; }
}

Okay. This is all about Auto-Implemented Properties (a.k.a Automatic Properties). If you have any suggestion or comment, please drop a comment in this post. Thank you!

C# 3.0 Tutorials with Visual Studio 2008 for Beginners

I have been doing C# 3.0 for a while now but I never wrote any tutorial related to C# 3.0 in my blog before. So, I decided to write a few C# 3.0 tutorials while I’m waiting the release of Silverlight 2.0 beta 1. I also notice that some of my friends from Silverlight Forum are so silent lately and they don’t even so active in the forum. I know that they are busy with preparing to go to MIX08 or learning WPF/Media/Flash/Flex.

In this tutorial series, I will cover the following topics with full of explanations, sample and etc (like my other tutorials in my blog). This tutorials are dedicatedly written for those who already have some experiences with C# 1 or 2 and totally newbie for C# 3.0. You will need to install Visual Studio 2008 to run the sample code.

Topics

The links will be updated once I published the tutorial in my blog. Please feel free to let me know if you want me to add something or if you have the suggestion or comment.

You can subscribe my feed if you used to read the blog through feed reader. OR, you can give me your email here then I will inform you if I post new tutorial in my blog. (Of course, I promise that I won’t share your mail with anyone and I won’t be spam you. You will have to activate your mail after putting your email address in this link.)

The Screenshot of Silverlight 2.0 (beta 1) Controls

I just got the screenshot of Silverlight 2.0 (beta 1) from this post “About Silverlight 2 Beta 1 Controls“. It seems that there will be 29 controls available in Silverlight 2.0 (beta 1) that support to be released in next month. That’s pretty good. You can check the hierarchical structure of SL 2.0 controls in this post.

Silverlight 2.0 (beta1) control

UPDATE

The most of controls are similar to WPF control so that we can imagine how those controls will look like. But MultiScaleImage control and Wartermarked Textbox control.

(Thanks to Pablo for sharing the link about MultiScaleImage and explaining about Wartermarked Textbox control)

  • WartermarkedTextbox: Some people call them ‘Prompting text boxes’. Essentially a textbox where the label is ‘watermarked’ in the actual textbox. When the user focusses or types inside the textbox, the watermark/prompt is hidden.

SEAMonster: Intelligent Image Resizing Tool

SEAMonster is the open-source .NET-based implementation of seam carving. Seam carving is an image resizing algorithm developed by Shai Avidan and Ariel Shamir. What that algorithm does is that it changes the dimension of image by intelligently removing pixels from (or adding pixels to) the image. If you are very interested in this algorithm, you may read it here or download this pdf file (20 MB).

Download : Executable or Sourcecode

SEAMonster Logo

You can watch the power of SEAMonster tool or how Seam Carving algorithm in the video below.

Video 1.1 : Seam Carving for Content-Aware Image Resizing

I noticed that tool since Mike Swanson, technical evangelist at Microsoft, wrote about this in his blog. (SEAMonster: A .NET-Based Seam Carving Implementation). I will write about how to use this tool in next article.

Related ~

(Links) – Silverlight 2/WPF Tutorials

I’m sorry that I’m not able to post any new post in my blog during these days. Because, those tutorials below eat all of my free time lately. but yes, those are petty good. You should read if you have some free time.
Silverlight 2.0 – Tutorials

Those tutorials are the first and earliest tutorials for Silverlight 2.0 that will be released next month. There is no sourcecode attached in those tutorials but it will be added once SL 2.0 is released. Yes, SL become more like WPF. :)

WPF: A Beginner’s Guide

Those following tutorials are written by Sacha Barber, one of my favorite writes from Codeproject. He is awarded MVP (Most Valuable Professional) from Microsoft and MVP from Codeproject. He wrote a lot of award-winning articles in Codeproject.

WPF Business Application Series

A Guided Tour of WPF

  • Part 1 (XAML): Learn about XAML and how it is used in WPF applications.
  • Part 2 (Layout): Learn about layout panels and how they are used to construct user interfaces.
  • Part 3 (Data binding): Learn how WPF data binding works and how it can be used.
  • Part 4 (Data templates and triggers): Learn how data templates and triggers work and how they can be used.
  • Part 5 (Styles): Learn about how UIs can be styled in WPF.

Plus, I’m also reading WPF – Unleashed book. I think that this book is also good one. If you are interested in learning WPF, I would recommend you this book. but only one thing. that books doesn’t focus on VS 2008 since it has been published before VS 2008 releases.

Download Flash file with RealPlayer

I wrote about how to download Flash file using Download Helper, the firefox addon, long time back. but I didn’t use that much since it doesn’t support some of my video sites that I want to download.

Recently, I downloaded the latest version of RealPlayer and subscribed the Superpass with unlimited access. I found that Realplayer is not only a powerful player but also the downloader that can download the movie (flash version) from any website with just one click. I think Realplayer is pretty impressive. I’m kinda like it.

Okay, back to the topic, I will tell you how to download the flash movie using Realplayer..

How-to?

  • Go to any movie website that you like.
  • Move over the movie object then “Download This Video” link will be shown at the top-right side of media player.
  • Click on this link

RealPlayer Download This Video

  • RealPlayer Download and Recording Manager will be shown as the picture below.

RealPlayer Download and Recording Manager

  • It will take some time depend on the internet connection that you use and other configurations.
  • If the downloading is completed, “Play” and “Remove” link will be shown. If you want to play the video, just click “Play” link at the right-side of RP D&R manager.

RealPlayer Download Complete

  • Realplayer will be opened and you can watch the video that you have downloaded. That’s all. Cool, isn’t it?

RealPlayer Open in Realplayer

You can find those downloaded videos under this path “C:\Users\(Your User Name)\Videos\RealPlayer Downloads\”. And also, you are able to change the path as you like in “Preferences” dialog of Realplayer.

I hope you are gonna like it. :) btw, Real Online Movie Library is not that good (at least for me) . I was looking for something like that but the problem is that they are not able to add new movies (not episode or TV-series) in their library. So, the idea for opening online movie library with low subscription fees is good but they can’t effort to make this happen until now.

Related ~