Saturday, October 25, 2014

C# Is Like Groovy, Not Java

A lot of people tell me that C# is similar to Java and, yes, the syntax for both languages was derived from C and they're both Object Oriented languages. However, in my opinion, I think C# is much more similar to Groovy than it is to Java.

multiple public classes, one source file
// Groovy
class Foo {d
}

class Bar {
}

println "Hello World"
Save as MyApp.groovy
Execute by typing
groovy MyApp
// C#
class Foo {
}

class Bar {
  static void Main() {
    System.Console.WriteLine("Hello World");
  }
}
Save as MyApp.cs
Compile by typing
csc MyApp.cs
Execute by typing
MyApp
name of source file != name of class
See above example
operator overloading
// Groovy
class Cookie {
}

class ChocolateChips {
}

class Dough {
  Cookie plus(ChocolateChips chocolateChips) {
     return new Cookie();
  }
}

Cookie cookie = new Dough() + new ChocolateChips();

assert cookie != null
assert cookie instanceof Cookie
// C#
class Cookie {
}

class ChocolateChips {
}

class Dough {
   public static Cookie operator +(Dough dough, ChocolateChips chocolateChips) {
      return new Cookie();
   }

   public static void Main() {
      Cookie cookie = new Dough() + new ChocolateChips();
 
      System.Console.WriteLine(cookie != null);
      System.Console.WriteLine(cookie is Cookie);
   }
}
properties feature
// Groovy
class Book {
   private String str;
   
   public void setTitle(String title) {
      str = title;
   }
   
   public String getTitle() {
      return str;
   }
}

Book book = new Book();
book.title = "The Stand";
println("Book's title is: " + book.title);
// C# example
class Book {
   private string _title;

   public string title {
      get {
         return _title;
      }

      set {
         _title = value;
      }
   }

   public static void Main() {
      Book book = new Book();
      book.title = "The Stand";
      System.Console.WriteLine("Book's title is: " + book.title);
   }
}
dynamic typing
// Groovy
def x = "Hello";
x = x + "World";
println x

x = 1
x = x + 2
println x
// C#
public class Klass {
   public static void Main() {
      dynamic x = "Hello";
      x = x + "World";
      System.Console.WriteLine("x = " + x);

      x = 1;
      x = x + 2;
      System.Console.WriteLine("x = " + x);
   }
}
import aliasing
// Groovy
import javax.swing.JFrame as FooBar

FooBar frame = new FooBar();
println "done";
// C#
using FooBar = System.Windows.Forms.Form;

public class Klass {
   public static void Main() {
      FooBar frame = new FooBar();
      System.Console.WriteLine("done");
   }
}
enums in switch statement
In Java, you don't prefix the element name in the case part with the name of the enum.
enum Days {
  Sat, Sun, Mon, Tue, Wed, Thu, Fri;
}
class Klass {
  static void main(String... args) {
    switch(Days.Tue) {
      case Tue:
        System.out.println("case Tue executed");
        break;
    }

    System.out.println("Finished");
  }
}
C#, on other hand, requires you to specify both the name of the enum and the name of the element you're matching on. Groovy behaves the same way.
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
public class Klass {    
  public static void Main() {    
    switch(Days.Sat) {
      case Days.Sat:
        System.Console.WriteLine("case Sat executed");
        break;
    }
  
    System.Console.WriteLine("Finished");
  }
}
verbatim strings
Both C# and Groovy allow you to create verbatim strings. These are strings that are displayed exactly as they are written. As consequence of this you don't escape backslashes.

To create a verbatim string in C# you prefix the string with an at sign. If your string needs a double quote, you must use two of them.
public class Klass {    
  public static void Main() {
    // System.Console.WriteLine(@"double quotes ""); // compiler error

    System.Console.WriteLine(@"backslash \, double quotes "", at sign @
  Second Line");
  }
}
Outputs the following:
backslash \, double quotes ",  at sign @,
  Second Line
To create a verbatim string in Groovy you use two forward slashes. If your string needs a forward slash then you must escape it.
class GKlass {
  static void main(String... args) {
    // println "backslash \\, double quotes \", forward slash /,\n  Second Line"; 

    // using verbatim string
    String str = /backslash \, double quotes ", forward slash \/,
  Second Line /;
    println str;
  }
}
Outputs the following:
backslash \, double quotes ",  forward slash /,
  Second Line
If you want to use a verbatim string on the left side of an assert statement, in Groovy you must surround the assert with parenthesis.
class GKlass {

  static void main(String... args) {
    assert "verbatim string" == /verbatim string/;
    // assert /verbatim string/ == "verbatim string"; // compiler error

    assert(/verbatim string/ == "verbatim string"); // this is okay
  }
}
null coalescing operator
According to Microsoft's C# Reference: "The null-coalescing operator returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand."

The null coalescing operator appears as two question marks in C#:
public class Klass {
  public static void Main() {
    System.Console.WriteLine("'" + (null ?? "Hello") + "'");
    System.Console.WriteLine("'" + ("" ?? "Salut") + "'");
  }
}
Outputs
'Hello'
''
Groovy calls its null coalescing operator the Elvis Operator because of the way it looks; a question mark followed by a colon.
println("'" + (null ?: "Hello") + "'");
In reality, the Elvis Operator checks if the expression on the left is false. Null values are coerced to false in Groovy.

See Also: Paul King's Groovy Truth
println("'" + ("" ?: "Salut") + "'");
Notice that the above outputs "Salut" in Groovy while in C# it outputs ''.

No comments:

Post a Comment