C# programming introduced to Objective-C programmers (Windows Phone 7) Part 1

In the previous topic, we looked at the user interface guidelines for WP7 applications. We will now dive deeper into what it takes to implement a WP7 application.

In this topic, we will look at the various C# features that map to the most common Objective-C features. We will provide code snippets which will ease your way into C# code. We will point to the key C# features that help you write safe code and enhance productivity.

Introduction to Managed Programming

WP7 only supports managed programming in C# or VB.NET. Before we jump into the details of C#, let us briefly review managed programming.

tmpC-36_thumb[1]

The C# compiler (and similarly, the VB compiler) compiles the C# (or VB.NET) code into an intermediate language (IL) bytecode and metadata. The Common Language Runtime (CLR) executes the byte code. It uses metadata to manage type safety, exception handling, array bounds, etc. The CLR also manages memory and performs garbage collection. In contrast, Objective-C code is compiled into ARM binary code and executed directly.


Comparison between C# Features and Objective-C Classes

Class Declaration

Let us start with an example program. In contrast to Objective-C, C# does not separate the class definition and the implementation. The compiler derives the metadata about the classes from the class implementation itself. You will also notice that you do not need to define each class in a separate file as in Objective-C.

In the example, the public signature of the class Person consists of just the property, age, and the constructor. The rest of the class implementation is opaque.

tmpC-37_thumb[1]tmpC-38_thumb

Instead of using the import statement, C# employs a using statement to refer to the metadata of other classes. The namespace declaration, shown at the top of the file, is used to both declare scope and organize the code. You can access classes in other namespaces by referring to a fully qualified name. See the reference to System.Console.WriteLine in the example above, where console is in the System namespace.

Objective-C uses a message passing syntax consisting of square brackets, and a dot-notation for accessing properties. C# uniformly uses the "." notation for referring to all methods, fields and properties.

Strong Typing

In contrast to Objective-C, C# is a very strongly typed language. Types must be specified for variables as well as input/output parameters. Types are enforced strictly by the compiler. Objective-C uses weak typing for collection classes such as NSArray and NSDictionary. In the section on generics below, we will see how C# uses strong typing for collection classes.

tmpC-39_thumb[1]

The example above shows the strong typing for primitive types. Strong typing works similarly for all classes.

Class Constructors

In contrast to the separate alloc and init statements of Objective-C, in C#, instance constructors are used to create and initialize instances. For example, p, an instance of the Person class, can be both constructed and initialized with a given birthdate, in a single statement.

tmpC-40_thumb

Properties

Developers often need to decide about whether to implement a member as a property or a method. In this case, the design pattern is identical for Objective-C and C#. In general, the guidance is to use properties when accessing data, and to use methods when there is an action taken on the data.

As opposed to the Objective-C @property attribute, C# properties are declared by the explicit definition of a getter, a setter, or both. You can make the property read-only by providing just the getter, write-only by providing just the setter or read-write, by providing both.

Parameter Types

Similarly to Objective-C, C# uses value parameters by default. While C# does not have pointers, it allows passing of parameters by reference by using the ‘ref’ modifier. Instead of pointers, parameters with ref can be used where you want to achieve side effects in a method. In some cases, reference parameters are more efficient, since they avoid data copying.

tmpC-41_thumb

C# also provides parameters with an out modifier which denotes parameters that must be initialized by the called method before returning. This design pattern is often used to return the error in addition to the value of the function.

Access Privileges

In Objective-C, access privilege can only be specified on variables. Methods which are present only in the .m file are private. On the other hand, C# allows access privileges on fields (e.g., birthDate), properties (e.g., age) and methods (e.g., ageOn). It uses public, private and protected as modifiers to denote three different levels of access privileges.

In the above example, the compiler will error out on p.birthDate since that variable is private and therefore is not accessible from the Program class. Similarly, the method ageOn is also private and inaccessible from the Program class.

Methods with multiple parameters

Both Objective-C and C# support methods with multiple parameters. In Objective-C method parameters are positional and named, i.e., the names of formal parameters are used while passing actual parameters. The name of the method is comprised of everything to the left of the colon (":"), for example, the name of the Objective-C method below is addEmployee:name:age:. While C# traditionally used positional and unnamed parameters, the latest version of C# has also introduced named parameters. The following example shows the comparative syntax for Objective-C and C#.

tmpC-42_thumb[1]

Objective-C does not support method overloading. While it does not allow exactly the same method signature with different parameter types, the following design pattern is commonly used in Objetive-C programs:

tmpC-43_thumb

As we saw earlier, the names of these two methods are different and are "insert:atIndex" and "insert:beforeObj" respectively.

On the other hand, C# explicitly supports method overloading. Using information about the parameter types, C# disambiguates between methods with the same name.

tmpC-44_thumb

The method insert may be called with both signatures:

tmpC-45_thumb

Now that we have examined some of the basic class concepts in C#, let us look at another example:

tmpC-46_thumb[1]tmpC-47_thumb[2]tmpC-48_thumb[1]

Next post:

Previous post: