Swift for C# Developers

Programming languages provide a way to communicate with a computer through notational instructions. Swift and C# are high-level languages commonly categorized under multi-paradigm and compiled programming languages. They not only fall into the same category of programming languages but also have many syntactical features in common. Due to this syntactical resemblance, C# developers can easily adapt Swift Programming Language for iOS, OS X, watchOS, tvOS, and Linux development. In this post, we will touch upon the key similarities between C# and Swift. You can try the swift code snippets in IBM Swift Sandbox, a tool that allows anyone to write and run Swift code from their browser.

Const is Let and Var is Still Var

A Constant is an identifier with a value which cannot be modified during the normal execution of a program. In C#, Constants are defined using keyword const and the syntax for defining a constant is

const {Type} {ConstantName} = Value; 
const int loginAttempts = 3;

In Swift, constants are defined using let and explicitly defining a type is optional as it picks the datatype based on the "value" which is provided.

 let {ConstantName} : {Type} = Value 
 let loginAttempts = 3
 let loginAttempts:Int = 3

A semicolon (;) at the end of each statement is optional in Swift. However, semicolons are required if you want to write multiple separate statements on a single line.

let loginAttempts=3; 
print(loginAttempts)

Note: In C#, When defining a constant symbol, its value must be determinable at compile time. Whereas in Swift, the value of a constant doesn’t need to be known at compile time, but must have a value assigned exactly once with explicit type defined. Similar to C#, var is the keyword to define a variable in Swift.

// C# - Implicitly typed local variable 
var name = "Sagar";

//Swift
var name = "Sagar"
var name : String = "Sagar"

Note: As C#, Swift is also type safe.It performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.

Classes and Objects

A class is a blueprint defining the data and behavior of a type. An object is an instance of a class created dynamically with a block of memory allocated. Properties, methods, and fields are the members of a class. In C#,

//class definition
class Employee
 {
     //Field
     private String _employeeName;

     //Property
     public String EmployeeName 
     {
       get{ return _employeeName; }
       set{ _employeeName = value; }
     }

     //Constructor or Initializer
     public Employee(String name)
     {
        this.EmployeeName = name;
     }

     //Method
     public String GetEmployee()
     {
       return EmployeeName;
     }

     //Destructor
     ~Employee()
      {
        //Cleanup Statements
      }
}

//Instance Creation - Object
var employee= new Employee("James");
var employeeName = employee.GetEmployee();

Similarly, in Swift

//Class Definition
class Employee()
 {
    //Property
    //Similar to a var but declared in a class.
    var name: String

    //Property with getter and setter
    var employeeName : String {
     get {
           return name
         }
     set {
           name= newValue
         }     
    }

    //Initializer or Constructor
    init(name: String) {
        //self is similar to 'this' in C#
        self.name = name
    }

    //Method 
    func getEmployee() -> String {
        return employeeName
    }

    //DeInitializer
    deinit
     {

     }

 }
//Instance Creation - Object
var employee = Employee("Jones")
var employeeName = employee.getEmployee()

Note: Methods are functions that are associated with a particular type. Also, if a method accepts more than one parameter, named arguments can be used the same way as in C#. E.g., var employee = Employee(name: "Jones", Id: 12345).

Class and Interface (Protocol in Swift) Inheritance

Inheritance is one of the primary pillars of object-oriented programming. Inheritance is all about reusability of the code. A new class can be created from an existing class.

// C# 
// Manager is termed as Sub / Derived class.
// Employee is termed as Super / Base class.
class Manager : Employee
{
   public Manager(String name) : Base(name) // Calls Employee(name) 

  //Method Overriding 
  //Only If the method in base class is marked as "virtual"
  public override String GetEmployee(){...}

}

// Swift
class Manager: Employee
{
   init(name : String) {
     super.init(name: name) // Calls Employee(name)
    }
   //Method Overriding
   override func getEmployee() -> String {...}
}

We can achieve multiple inheritances through Interfaces in C# and protocols in Swift. interfaces and protocols are just contracts without any implementation. The syntax is similar to the class declaration. In C#,

// Interface Definition
Interface IProjectManager
{
   String projectName{ get; set;} 
   Int GetDeveloperCount();

}

//implementation
class Manager : Employee, IProjectManager
{
   //Field
   private String _name;

   //Property
   public String projectName{
     get{
         return _name;
        }  
     set{
         _name=value;
        }

   //Method 
   public Int GetDeveloperCount()
    {  
        return 15;
    }
}

In Swift,

//Protocol Declaration
protocol ProjectManager() 
{
   var projectName: String {get set}
   func getDeveloperCount() ->Int 
} 

//Implementation
class Manager: Employee,ProjectManager
{
  //Simple Property
  var name: String

  //Property with getter and setter
  var projectName: String {
     get {
            return name
         }

     set {
           name = newValue
         }
    }

   //Method
    func getDeveloperCount() -> Int {
       return 15
    }
}

Lambdas are Closures in Swift

Lambda expression introduced in version 3.0 of C# language is an expression which returns a method. The expression lambdas are expressed in the following basic form in C#,

(input parameters) => expression

In Swift, the Closure expression takes the following general form,

{ (parameters) -> return type in statements }

Sorting a list of names is a herculean task for any developer. So here's an example showcasing the beauty of lambdas/Closures.

In C#,

Note: Just for the sake of comparison and understanding, I am using a C# console application for the code snippets below.

 class Program
    {
        static void Main(string[] args)
        {
            //List of names 
            var names = new List{ "John","Johnes","Aria","Amar","Rajesh","Xavier"};
            names.Sort((s2, s1) => s2.CompareTo(s1));

            foreach (var name in names)
            {
                Console.WriteLine(name);

            }
            Console.ReadLine();
        }
    }

Once you run the code in Visual Studio, You should see an output as shown in the figure below.C# Console application output. C# Console application output.

In Swift,

//List of Employee names
var names=["John","Johnes","Aria","Amar","Rajesh","Xavier"]

//Closure Expression
names=names.sort({(s1: String ,s2: String)-> Bool in return s2 > s1})

// for-in loop
for name in names {
    print(name)
}

Swift output as seen in IBM Swift Sandbox (in a browser)IBM Swift Sandbox IBM Swift Sandbox


Generics are Generics in Swift

C# provides generics to remove the need for casting, improve type safety, reduce the amount of boxing required, and make it easier to create generalized classes and methods. The generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. Generics in C#,

class Program
    {
        //Generic method
        static void SwapTwoValues<T>(ref T a, ref T b)
        {
            T temp;
            temp = a;
            a = b;
            b = temp;
        }
        static void Main(string[] args)
        {
            // Swap two values
            int a = 40, b = 60;
            Console.WriteLine("Before swap: {0}, {1}", a, b);

            SwapTwoValues<int>(ref a, ref b);

            Console.WriteLine("After swap: {0}, {1}", a, b);

            Console.ReadLine();
        }
    }

 Generics in Swift,

///Defining a generic function to swap two values
func swapTwoValues(inout a: T, inout _ b: T) {
    let temporaryA = a
    a = b
    b = temporaryA
}

//Usage of swapTwoValues
var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
// \() - String Interpolation
print("After Swap:\(someInt),\(anotherInt)")

Note:

Along with the features mentioned above, Swift provides some power features which makes coding smooth and bug-free.

What we discussed till now is just a drop in the ocean. There are many features which have similar syntax in C# and Swift. Structs, enums, loops (control flow) are few of them which you can explore and if you are struck anywhere feel free to drop a comment in the comments section below.

What's Next?

Check Official Swift Documentation

Happy SWIFTing!!!

 

 

 

 

Top