Software Design Engineer since 2001
Archive for September, 2010
New Property System and Property Extensions in C# 5.0
Sep 9th
Programming languages evolve rapidly. New great features have been added to one of my favorite languages C# – lambda expressions, extension methods, dynamic type and many others. What can we expect next?
One thing that comes to mind is a more advanced property system. WPF introduced a notion of DependencyProperty which has proved its success. But.. can C# support something similar natively – properties which could store their states and dynamic meta data in addition to plain values? Why not?
Consider the following code sample:
// A product type in this sample has a property Name of type String
var product = new Product();
// This will print "Product.Name.IsDirty: False"
Console.WriteLine("Product.Name.IsDirty: {0}", product.Name.IsDirty);
// Subscribe to an event
product.Name.BeforeChange += (s, e) => {
if (e.NewValue == "C++") { e.Cancel = true; }
};
// Now let's try to update product's Name property
product.Name = "C# 4.0 in a Nutshell";
// And now this will print "Product.Name.IsDirty: True"
Console.WriteLine("Product.Name.IsDirty: {0}", product.Name.IsDirty);
// Then when you try to save this product in database, it will be easier
// for your Store Repository to decide which fields must be saved
// and which can be skipped
storeRepository.Add(product);
storeRepository.SubmitChanges();
So far so good? But.. how this Product class is implemented? Actually not a magic.
Here is one of possible implementations – Codename “Properties inside properties” or “Nested Properties” in short:
public interface IProduct
{
string Name { get; set; bool IsDirty { get; } }
}
public class Product : IProduct
{
private string name;
private bool nameIsDirty;
public string Name
{
get { return name; }
set { if (name != value) { name = value; nameIsDirty = true; } }
bool IsDirty { get { return nameIsDirty; } }
}
}
Happy coding!
P.S.: To tell you the truth, this is not the brightest example of what to expect in the next version of C#. But if this blog post motivated you to creative thinking – then my goal is completed. Don’t be afraid to innovate; be different!
Teaching programming language concepts with F# at Channel9
Sep 2nd
Teaching Programming Language Concepts with F#, Part 1
In this first part, Peter introduces the curriculum, lecture plan and lecture notes for the course “Programs as data” that uses the functional programming concepts in F# to teach students language concepts and implementation details.
Teaching Programming Language Concepts with F#, Part 2
In this second part, Peter finishes the first “demo” lecture of the F#-based programming language course.