Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

5 Reasons .NET Devs Should Try Xamarin

DZone's Guide to

5 Reasons .NET Devs Should Try Xamarin

It turns out that C# will take you a long way with Xamarin. If you're a .NET dev, you'll have a leg up with native UIs and the ability to cross platforms.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

If you’re a .NET developer who’s ready to dive into the mobile world, Xamarin is the tool for you!
Xamarin is an open source framework that allows developers to create mobile applications for Android, iOS, and Windows Phone written in a single language: C#. This is great because you don’t have to spend time learning native platform languages such as Java, Swift, or Objective-C. Instead, you can open up Visual Studio (or Xamarin Studio in Mac or Linux), set up Xamarin, and start coding your cross-platform application in C#!

The applications not only look native but they behave that way, too. That’s because the C# code is compiled into native code, so your application feels and runs like one developed with native technologies. There are many well-known companies (think Slack, Pinterest, and JetBlue) that are using Xamarin as their mobile development platform.

In this article, I’ll walk you through the main benefits of using Xamarin and why you should consider it for your next mobile project.

Native User Interface, Native API, and Native Performance

Xamarin applications are compiled to native code and run natively on the target device. This provides far greater than you could achieve with applications backed by code that is interpreted in run-time, even in games where performance is crucial.

The application interface is also compiled with native user interface controls. This means the user experience you get on a Xamarin application is the same you would expect from an application developed with native technologies. Actions like swipe or scroll will feel fluid and natural.

Xamarin also allows you to interact with most of the underlying platform API. It provides you with strongly typed bindings for almost all of the native SDKs, both in iOS and Android. It also allows you to invoke libraries written on native technologies (Java, Objective-C or even C/C++) so you can reuse already written code on those technologies in your application. There is no technical barrier to what you can achieve with Xamarin!
Xamarin

Xamarin.Forms for Native UIs

Xamarin.Forms is a toolkit that allows developers to build native user interfaces that can be shared across Android, iOS, and Windows Phone without having to become an expert in the nitty-gritty details of each platform. Xamarin.Forms lets you reuse almost all of your code: You write only one interface for all the platforms. You can create interface pages either in C# or XAML. Each page represents a single screen in your application. You can use a wide variety of user controls to customize your interface as much as you like. Then, at run-time, the pages are mapped to platform specific native user controls. This means that the interfaces will have the native “look and feel” of each platform.

When you need to add platform specific functionality (i.e. to use platform-specific APIs or interactions), you can use Xamarin.Android or Xamarin.iOS to tune up the interface created with Xamarin.Forms and cover those needs.

Easy Integration with Visual Studio

If you’re a .NET developer, chances are your IDE of choice is Visual Studio. Setting up Xamarin for Visual Studio is really simple: Download the Visual Studio package and run the installer. This will set up and configure all you need in order to develop with Xamarin on Visual Studio.

Xamarin also provides you with Android and iOS interface designers. These work pretty much like the Windows Forms designer: They let you build your application interface by dragging and dropping controls on a visual representation of the device screen. It’s really simple and effective!

You can also launch and debug your applications within Visual Studio, even iOS applications! There’s an iOS simulator for Windows (check it out here). And of course, you can use all of Visual Studio features when working with Xamarin, like shortcuts, intellisense, etc.

C# Across All Platforms

If you’re a C# lover like me, you’ll be happy to know that you can use its awesome features in your Xamarin applications, such as first-class functions, dynamics, LINQ, generics, type inference, and async/await to keep your applications responsive.

The C# code can be re-used across all platforms. You can write your business logic, database layer, common functions, and even the user interface (using Xamarin.Forms) and re-use them on each platform. Furthermore, you can use existing libraries you are familiar to work with, like SQLite.NET and NewtonSoft.Json.

Shared Code in Xamarin

As I mentioned before, with Xamarin you can share code across all platforms. There are two approaches, Portable Class Libraries and Shared Projects.

PCL: Portable Class Library

In a regular library project, the resulting DLL works only on the specific platform it was created for, but with a PCL you can choose a combination of platforms for your code to run on, and write code in a single project that can both be referenced in each platform-specific project and consumed by other libraries or applications.

Recently Xamarin added support for PCLs to consume .NET Standard libraries. .NET Standard is a specification for .NET APIs available on all .NET Runtimes (including .NET Core and ASP.NET Core platforms), and is intended to be the next generation of PCLs. This allows developers to create PCLs that are usable across different .NET runtimes using the same set of APIs.

Both the .NET Standard Library and PCL’s were created for the same purpose, but they have some differences. PCL profiles are defined by the intersection of existing platforms, and each profile represents a different Microsoft Platform. On the other hand, .NET Standard Library is platform-agnostic.

Today, you cannot create .NET Standard Libraries in Xamarin Studio on macOS, but it’s a functionality that’s coming soon!

Whether you are using .NET Standard or PCLs, the benefit of having a centralized code is that if you have to make changes or refactor your code, you can do it in a single place and it will affect not only the PLC you are modifying but also the platform-specific projects.

Shared Projects

In contrast to PLC, a Shared Project doesn’t have an output. Instead, the code is compiled into each project that references it. In order to write platform-specific functionality, Shared Project allows you to include compiler directives, eg. #if __ANDROID__, into the shared code that will enable or disable sections of the code depending on the project target platform.

Both options have pros and cons, so check the official documentation to see which one best suits your application.

Need More Reasons?

Xamarin has been proactive about staying up to date with the latest platform releases. It has a big community and there is a lot of documentation available on the web, so what are you waiting for? Start learning!

Did I mention it’s free and open source? Thanks, Microsoft!

If you’re ready to try Xamarin on Visual Studio, Nate Barbettini will get you started in this recent tutorial. To learn more about Xamarin, check out these resources:

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
xamarin ,.net ,c# ,mobile development ,mobile

Published at DZone with permission of Laura Rodriguez. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}