2017-05-30

Some thoughts on Java and C#

In a couple of weeks I will be giving a talk about best practices in native Android app development using Xamarin during Developer Week 2017 in Nuremberg. So, time to freshen up my C# knowledge. Care to join me? Fine. Let's take a look at a very basic Android activity, written in Java:

package com.thomaskuenneth.testapp1;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

The Java™ Tutorials explain:
A package is a namespace that organizes a set of related classes and interfaces. 
To which package a class or interface belongs is specified using the package keyword. As you can see in my example, such names can be rather long (com.thomaskuenneth.testapp1). A package name follows some conventions, for example it resembles domain names in reverse order. Also, it is lower case only. To free the developer from the hassle of having to type it every time a class is referenced, the import statement may be used. In the line starting with public class I refer to Activity, whose fully qualified class name is android.app.Activity.
The extends keyword makes MainActivity a child of Activity. As Java is a single inheritance language, there is only one direct ancestor. @Override is an annotation. It documents that the method onCreate() is present in the parent class, too. super.onCreate() invokes the implementation of the parent.
Finally, protected is an access modifier that controls who (that is, objects) can call that method. Now, let us turn to C#.

using Android.App;
using Android.Widget;
using Android.OS;

namespace TestApp1
{
    [Activity(Label = "TestApp1", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);
        }
    }
}

C# has the concept of namespaces, too. In my example a namespace called TestApp1 is declared. Real-world-applications should follow Microsoft's namespace conventions. To reference other namespaces, the using directive is used. However, Android.OS is not a class but a namespace. To achieve a similar effect in Java one would write import android.app.*;. If the wildcard should be used still is debated heavily, at least now and then. Before moving on, I feel the need to remind you that the mechanics in Java and C# are not equal, so please refer to the language specifications of C# and Java for further detail. In particular, C# knows something like

using (MyResource myRes = new MyResource())
{
    myRes.DoSomething();
}

This ensures that myRes is disposed automatically as soon as it is no longer accessible. try-with-resources, anyone? :-)

Inheritance is documented in C# with a colon. MainActivity is a direct descendent of Android.App.Activity. What super is for Java, is base for C#. Both refer to the direct ancestor.

Have you spotted the line starting with [ and ending with ]? Here we see so-called attributes. Frankly, they remind me of annotations, but I would love to hear someone comment on this. See the docs for further reference. Finally, a minor, yet important difference: remember when to start something with a uppercase letter. In Java it is setContentView() and onCreate(), whereas in C# we have OnCreate() and SetContentView().

There are more similarities and differences. Which of then do you find hard to remember? Tell me, I'd love to hear them...

No comments:

Post a Comment