Wednesday, June 11, 2014

[Visual C# - Apple Swift] Comparison between Visual C# and Apple Swift

As you might have seen Apple has recently released a beta version of a new programming language called Swift (see here for more information). This new language is going to replace Objective-C for developing OSX and iOS applications. It is very similar to C# and I would like to show you a high level comparison of its features when compared to our preferred C# language. I have used the official Apple Language Guide for Swift to do the comparisons, which you can find here.

Data Types & Declarations

One of the first things you have to learn when beginning a new language is what data types are provided and how to declare constants and  variables. Lets see how it is done for both languages by looking at some basic examples.

In the Swift language constants are declared by using the let keyword, whereas variables are declared by using the var keyword. Type inference is supported and you can declare a specific type by using type annotations.

image

image

In the C# language constants are declared by using the const keyword, whereas variables are declared by using the var keyword. Type inference is supported and you can declare a specific type by using it instead of the var keyword.

image

Both languages support the use of Unicode characters within variable and constant names and they both have the same syntax for code comments.

image

You see that they are quite similar in how to handle constant and  variable declarations, there are very little differences. Nonetheless there is a big syntax difference that you might have spotted already: the Swift language does not require a line termination with semi-colons. It supports it if you need to put multiple operations in a single line, but it does not require it when you work in a standard way.

There are other similarities such as how to define Integer boundaries or how to work with Strings. They both provide min and max properties for Integers for example.

Swift:

image

C#:

image

They both have a similar approach on how to compare Strings and even some of the provided String methods are very alike:

  • hasPrefix() –> StartsWith()
  • hasSuffix() –> EndWith()
  • uppercaseString() –> ToUpper()
  • lowercaseString() –> ToLower()

Swift:

image

C#:

image

When looking at Arrays and Array Literals as well as Dictionaries and Dictionary Literals, we can see that both languages have a very similar approach, even the syntax is nearly the same.

Swift:

imageimage

C#:

imageimage

Control Flow

Both languages provide mostly the same control flow constructs:

  • for and while loops to perform actions multiple times
  • conditional if and switch statements to execute different code branches
  • break and continue statements to transfer flow execution

Again the Swift syntax bears a strong resemblance to the C# syntax. Both languages implement the traditional for-condition-increment loop as well as the more modern foreach/for-in loops, which makes it easier to loop over collection types such as arrays/lists, dictionaries, ranges, strings and sequences.

Swift:

image

image

C#:

image

image

Both languages provide the classic while and do-while loops. There are no significant syntax changes or functional differences between Swift and C# to speak of (the only visible difference is that there are no parentheses in Swift around the conditions). The same applies to the if-then-else, switch, break and continue statements.

 Swift:

image

image

image

C#:

image

image

image

Conclusion

You have seen that Swift has implemented more or less the same syntax approach that C# provides for a long time now. I think that C# developers can quickly switch over to Swift if needed. But given the fact that Xamarin allows to develop efficient applications for the OSX and iOS world using C#, I doubt that this will be necessary.

Apple has modernized its main programming language, but there is still a long way to go to concurrence all the fairly advanced language features of C#. I think it is a good thing that those languages converge, we might as well motivate Swift developers to use C# in the future (to convert them), since it will be very easy for them to switch over.

Note that this is by no means a complete comparison, I just wanted to give you a quick overview. There is so much more to compare, so please try it out for yourself and share your feedback.


Share/Save/Bookmark

No comments: