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

Monday, June 9, 2014

[Visual C# - Unit Tests] How to extend NFluent and make it even more powerful

 

In my last article on NFluent I have shown you how it can help you to easily write clear and meaningful unit tests. In this article I am going to explain how you can extent NFluent by adding new custom validations.

What are DataAnnotations and how to use them ?

The System.ComponentModel.DataAnnotations namespace allows to define constraints on classes, methods and properties. DataAnnotations are often used within application based on ASP.NET MVC and Entity Framework for validation purposes.

The example below shows how to constrain user objects, with the following rules : All user objects have mandatory ID and Name properties, while the value of the ID property has to be in-between 1 and 999 and the Name property cannot exceed 30 characters.

image

DataAnnotations are widely used within the .NET Framework and I am happy to announce that NFluent uses them for extending its functionalities. So nothing new to learn for developers, who are already using them in their daily work!

Extend NFluent by adding new custom validations

It is very easy to add new custom validations to NFluent, you just have write an extension method and add the new custom verification code, as you can see in the following example implementation :

image

Note that a FluentCheckException is returned in case of validation errors.

How to use the custom validation in your code

Now that you have added your custom validation code, you can use it within your unit tests. You just have to call the DataAnnotationsAreValid() method :

image

Conclusion

This article has shown how easy it is to extent NFluent by adding custom validation code and then using it within your unit tests by just doing a single method call. There are obviously other ways to extend NFluent, but this simple and quick example is already sufficient for most scenarios.


Share/Save/Bookmark

Thursday, June 5, 2014

[VS2014] Visual Studio 2014 CTP has been released

The next version of Visual Studio named “14” has been released as CTP. There are some interesting new features, but this is a very early version of VS 2014, so do not expect too much right now.

You can download it from here: Visual Studio “14” CTP and MSDN Download

There is already a Virtual Machine in Azure, that you can use to evaluate all the new features : VM in Azure

Here is a list of the new features:

  • The .NET “Roslyn Compiler Platform
  • Visual C++ Standard Library updates:  added utility functions, manipulators, functions, and several bug fixes which are going to improve your productivity.
  • ASP.NET vNext Tooling:  ASP.NET vNext is a lean and composable .NET stack for building modern web applications for cloud and on-premises scenarios.

Please read the official blog posts and release notes to get more detailed infos on what has been added:

The CTP should be installed in a testing environment as a Virtual Machine (you may use the Azure VM). Do not install it side-by-side on a machine with another version of Visual Studio. Do not try to update an existing version, there might be unexpected results.

This is still a very early preview version so do not install it on production system or use it to develop your production code. There is no support from Microsoft and features might and will change in the final version.

Please feel free to leave your comments and to tell the other readers what you think of this new version of Visual Studio.


Share/Save/Bookmark