Skip to content

Archive

Tag: C#

Both Java and Mono (the open source implementation of C# and .NET) will be receiving pretty big updates this year. These represent two very popular virtual machine based coding platforms that offer portability and a slew of helpful standard libraries. Here are just a handful of the improvements each will see in the coming release.

Java SE 7.0

While no tentative release date has been scheduled for Java 7 yet, the team has recently completed milestone 10 of 10. Sacrificing some features (which will be delayed until Java 8.0) Java 7 still has quite a few intriguing new additions.

  1. G1 (new garbage 1st garbage collector)

    This new garbage collector improves upon the original generational one by introducing the concept of regions. You can think of regions has smart versions of the generational lists that should allow the collector to clean up large chunks of garbage with increased efficiency.

  2. Dynamic language support

    The JVM will now support some internal changes which will allow dynamic languages (like Perl and Ruby) to leverage the speed and widespread nature of JVM for languages that aren’t strongly typed like Java. This in turn will will let dynamic languages have hooks into the standard Java libraries as well.

  3. NIO (New I/O)

    A new set of libraries for dealing with I/O. This will let Java do some interesting things like create symbolic links as well as giving general I/O operations a big performance boost.

  4. Native Java video libraries

    New standard video libraries will let the JVM playback and record video sources on any platform. No more relying on external libraries to do the job.

  5. Strings in switch statements

    I for one never really understood why you weren’t allowed to do this before. Obviously switch statements are highly optimized to act on numeric values but would it really be that hard for the compiler to change the string switch statement into an if statement for me? Now I don’t have to even think about it anymore.

  6. Automatic resource management

    Rather than having to worry about closing that I/O stream, Java will now take care of it for you. The new syntax is:do(BufferedInputStream bis = …) { //do some work} //it will automatically be closed here

  7. Others

    Other improvements include compressed 64-bit pointers to save memory, new parallel processing libraries to leverage multi-core CPUs and graphics acceleration improvements.

Mono 2.28

The folks over at the Mono team just finished up a new release that contains quite a few interesting additions as well.

  1. Full C#/ASP.NET 4.0 support

    This is nice to see because it means at the very least Mono and .NET are on equal footing when it comes to language syntax.

  2. New generational garbage collector

    Moving away from the older Boehm-Demers-Weiser garbage collector to a new generation garbage collector (a la Java) should mean improved performance at the temporary expense of extra memory usage. This should let the runtime decide the best time to schedule a garbage collection action and only focus on the objects most likely to need collection.

  3. Dynamic language support

    Similar to Java, this version of Mono now also supports the System.Dynamic libraries. Again this will allow dynamic languages to leverage the portable nature of Mono as well as hooking into the various Mono runtime libraries.

  4. New parallel libraries and frameworks

    These new libraries should make it even easier to create applications that make use of parallel processing. Among the libraries is PLINQ, an optimized version of LINQ for multi-core systems.

  5. Overall performance improvements

    Mono now targets LLVM which should make it better as a server platform. In addition there have been a lot of code fixes and performance improvements throughout the project. Finally this version of Mono has also removed Glib as a dependency which should make it lighter and more portable.

For my iPhone Application Programming course I have become quite accustomed to using Objective-C; mostly because Apple strongly recommends requires that you write all of your code in it. Let me just begin by saying that Objective-C can be one of the most confusing and, at least at first glance, poorly designed programming languages that I have come across. Rather than using the standard C-like syntax of instance.method Objective-C uses a message passing syntax which looks a little something like [instance method]. Or… at least it used to. With the introduction of a new feature set Objective-C has also gained a ‘dot’ syntax similar to more classical languages. See what I mean about confusing?

So why on earth would anyone program in this language? Well in my opinion there are a number of good points that make Objective-C an ideal language to use for certain scenarios.

  • It is one of the few high-level languages that still compiles to native machine code.
  • It was built from the ground up as an object oriented programming language. This stands in contrast to C++ which effectively tries to tack OOP onto classic C.
  • Because it wasn’t trying to preserve any backwards compatibility, as was the case with C++, Objective-C did not inherit problems of an earlier language.
  • Objective-C can interface with standard C libraries and can even include C code inline for ease of use.
  • While Objective-C does make use of pointers, it does not suffer from the ‘pointer hell’ that C/++ does. What I mean by this is that it is more intelligent about its use of notation. For example, you don’t need to remember that you need to grab the memory address of an object (&) and then pass that as a pointer (*) to a function or, god forbid, make use of a pointer to a pointer (to a pointer to a pointer…).
  • Like C/++, Objective-C gives you complete control over memory management. However if you choose to you can enable automatic garbage collection for your code as well.
  • Unlike C’s #include pre-compile directive,which always forces a full copy of the source at be added at that point, Objective-C’s #import directive only adds the source once resulting in a smaller footprint.
  • When you finally do get Objective-C’s syntax it becomes a very straightforward and easy to read language.

Setup

On the Mac setup is basically as easy as installing Xcode. If you happen to use that platform I would highly recommend that you use Xcode as it is the absolute best Objective-C IDE. However for the rest of us we get to dig into the command-line!

Following this excellent guide I was able to install the required libraries and tools for both Windows and Linux. Essentially just follow the instructions on www.genustep.org, the open source implementation of NeXT’s Objective-C libraries, and you should be off to the races. Just remember that for Windows you only actually need to install GNUstep System and GNUstep Core (in that order!). Once everything is installed just open up your terminal (Linux) or start GNUstep Shell (Windows). This is where we will be compiling the programs from.

First program

Everyone needs a classic ‘hello, world!’ example program! Here is a very simple way to do this in Objective-C. Create a file called main.m (.m is like .c or .cpp) and place the following code in it:

//Link into the Foundation library which contains a lot of useful functions
#import <Foundation/Foundation.h>

//the main program function that will be run
int main(void)
{
     //log our string out to the console
     NSLog(@"hello, world!");

     //return success exit code
     return 0;
}

Then to compile it follow the instructions back on this post. I have found that for me the best Windows command line arguments are as follows:

gcc `gnusetup-config –objc-flags` -o [output name i.e. "test" produces "test.exe"] [input .m files i.e. "main.m"] -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -enable-auto-import

Your results may be different though. For the above example I used “hello” as the output name and “main.m” as the input .m files. On my machine this resulted in a file called hello.exe that was 478KB in size. If we change the first line of the program to #import <Foundation/NSString.h> instead and then recompile it we can shrink this down to 417KB. This is because we only really use the string library in our program, so importing the rest of Foundation is simply unnecessary.

Using objects

Rather than write a whole tutorial here I have created a little demonstration about the different ways you can use objects. While I don’t claim that this is the best way to do things, in fact if you read main.m I claim quite the opposite, it does give you an idea of the flexible nature of Objective-C’s OOP and memory management systems.

Download Here

Wrapping it up

Essentially what I am trying to get across here is that Objective-C is far from perfect, but it is quite a mature language and as such should probably have more wide-spread support. As a Computer Scientist who grew up in the post-C++ world, see: Java, I completely understand why many people are opting to use sandboxed or ‘virtualized’ programming languages, like .NET, Java and even things like PHP, over native languages like C/++, Objective-C or assembly. On the other hand there is something to be said for still writing code on the metal if for nothing else than the performance improvements you get by default. A lot of people are afraid of C’s pointers but if you can program in Java then I think you can easily program in Objective-C.

Give it a shot, you just might like it ;)

I honestly don’t remember how I came across this awesome project but I am certainly glad I did! XMLVM is a software toolchain which is designed to take cross-compilation to a whole new level. Rather than just offer OS portability, XMLVM is able to actually offer OS, hardware and programming language portability.

Here’s how it works: you write a program in a programming language of your choice, say .NET. Once compiled you send it through the first step of XMLVM which analyzes the produced CIL and creates an XML document out of it. It would end up looking like something similar to this:

<clr:ldc type=”int” value=”2″/>
<clr:rem/>

Next this XML document is fed through what XMLVM calls the data-flow analysis (DFA). Basically you can think of DFA as a pseudo-language that simply describes the operations that the program is trying to perform. Once in this form the code is considered portable. XMLVM then lets you pick a target, for example the Java JVM, and automates the conversion of the DFA to an XML representation of the java byte code. From there it’s an easy conversion back to true java byte code.

Now think about this in practical terms for a second. That means that you can write a program in a .NET language (C#), and have it automatically ported and compiled to Java. Expand on this a bit and consider that you can write the same program in any language and have it converted to any other language. Currently the XMLVM offers a lot of other cool options as well and has actually been designed a lot with mobile devices in mind. Now you can write a program once and have it automatically converted to Objective-C, to run on the iPhone, and to Java to run on Android.

I really hope that this project continues to improve and I will certainly be watching it closely. It is still very early in development but from what I have seen it is simply brilliant.

That’s right an update to your favourite hash verification program! :P

This update includes a few new features that some of you might find useful. It also includes help documentation which walks you through how to use it!

New Features

  • Menu strip for even easier use
  • Export features allows you to automatically write all of the hashes to a single file
  • About dialog that provides information about the program
  • Help documentation

Requirements:

  • All platforms: .NET 2.0+ / Mono, a graphical display
  • *nix platforms: WinForms (identified as System.Windows.Forms)

As always the binary only package contains just the executable, whereas the all package contains the source code as well.

Binary Only Package All Package
File name: hash_verifier_0_2_0_0_binary.zip hash_verifier_0_2_0_0_all.zip
File hashes: Download Here
GPG signature: Download Here Download Here
Screenshots: Screenshot 1 Screenshot 2
License: (LGPL) View Here
Version: 0.2.0.0
File size: 171.5KB 530.1KB
File download: Download Here Download Here

As someone who has recently begun to experiment with the Linux operating system I have also been introduced to .NET’s Linux’s cousin Mono. This has made me question what the best cross-platform program language to use is. I am familiar with both Java and various .NET languages (Visual Basic & C#) so I decided to run a few tests to see what the resource usage on my Linux laptop is like between these two competing platforms.

Hardware

CPU Information

  • Processor (CPU): Intel Core2Duo CPU P8600 @ 2.40GHz

Memory Information

  • Total Memory (RAM): 4GiB
  • Swap Partition Size: ~6GiB

OS Information

  • OS: Linux 2.6.30.9-102.fc11.x86_64
  • System: Fedora  11
  • Desktop Environment: KDE 4.3.3

Display Info

  • Model: ATI Mobility Radeon HD 4670
  • Driver: 2.1.9026 FireGL

Hard Drive Info

  • Memory: 320GiB 7,200 RPM SATA

Software

I am using Java version 1.6.0 and Mono version 2.4.2.3 for these tests.

Test Setup

For the following tests I have provided source code for both the Java and the C# implementations. I then ran the programs and checked their CPU and memory usage.

Test I

Simple write/read test to the console. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestI {

     public static void main(String[] args) {

          System.out.println(“Hello World”);

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestI
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               Console.WriteLine(“Hello World!”);

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }

          }
     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test II

Simple primitive variable usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestII {

     public static void main(String[] args) {

          byte b = 0;
          short s = 0;
          int i = 0;
          long l = 0;
          float f = 0;
          double d = 0;
          boolean bo = true;
          char c = ‘a’;

          //set them all to something
          b = 1;
          s = 1;
          i = 1;
          l = 1;
          f = 1;
          d = 1;
          bo = false;
          c = ‘b’;

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }

     }

}

C# code:

using System;

namespace TestII
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               byte b = 0;
               short s = 0;
               int i = 0;
               long l = 0;
               float f = 0;
               double d = 0;
               bool bo = true;
               char c = ‘a’;

               //set them all to something
               b = 1;
               s = 1;
               i = 1;
               l = 1;
               f = 1;
               d = 1;
               bo = false;
               c = ‘b’;

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.1MiB
CPU Usage: 0% 0%

Test III

Simple primitive variable usage, this time with arrays. Same as above but with arrays of 10,000 for each primitive. The arrays were set using loops similar in structure to the following:

for(int x=0; x<10000; x++)
{
     b[x] = 1;
}

Results:

Java C#
Memory Usage: 5.9MiB 2.4MiB
CPU Usage: 0% 0%

Test IV

Simple object usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestIV {

     public static void main(String[] args) {

          SimpleClass sc = new SimpleClass();

          sc.setID(5);
          sc.setName(“Hello World”);

          System.out.println(sc.getString());

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

public class SimpleClass {

     private int _ID;
     private String _name;

     public SimpleClass()
     {
          //default constructor
     }

     public SimpleClass(int ID, String name)
     {
          _ID = ID;
          _name = name;
     }

     public int getID()
     {
          return _ID;
     }

     public void setID(int ID)
     {
          _ID = ID;
     }

     public String getString()
     {
          return _name;
     }

     public void setName(String name)
     {
          _name = name;
     }
}

C# code:

using System;

namespace TestIV
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               SimpleClass sc = new SimpleClass();

               sc.setID(5);
               sc.setName(“Hello World”);

               Console.WriteLine(sc.getString());

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

using System;

namespace TestIV
{

     public class SimpleClass
     {

          private int _ID;
          private String _name;

          public SimpleClass()
          {
               //default constructor
          }

          public SimpleClass(int ID, String name)
          {
               _ID = ID;
               _name = name;
          }

          public int getID()
          {
               return _ID;
          }

          public void setID(int ID)
          {
               _ID = ID;
          }

          public String getString()
          {
               return _name;
          }

          public void setName(String name)
          {
               _name = name;
          }

     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test V

Simple object usage, this time with arrays. Same as above but with arrays of 10,000 for each object. The arrays were set using loops similar in structure to test III:

Java code:

import java.io.IOException;

public class TestV {

     public static void main(String[] args) {

          SimpleClass[] sc = new SimpleClass[10000];

          for(int x=0; x<10000; x++)
          {
               sc[x] = new SimpleClass();
               sc[x].setID(5);
               sc[x].setName("Hello World");

               System.out.println(sc[x].getString());
          }

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestV
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               SimpleClass[] sc = new SimpleClass[10000];

               for(int x=0; x<10000; x++)
               {
                    sc[x] = new SimpleClass();
                    sc[x].setID(5);
                    sc[x].setName("Hello World");

                    Console.WriteLine(sc[x].getString());
               }

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 10.1MiB 2.6MiB
CPU Usage: 8% 2%

Test VI

Everyone’s favourite bubble sort with 10,000 integers.

Java code:

import java.io.IOException;

public class TestVI {

     public static void main(String[] args) {

          int[] array = new int[10000];

          //fill array
          for(int i=0;i           {
               array[i] = 10000-i;
          }

          boolean swapped;

          do
          {
               swapped = false;

               for(int i=0;i                {
                    if(array[i] > array[i+1])
                    {
                         int temp = array[i+1];
                         array[i+1] = array[i];
                         array[i] = temp;

                         swapped = true;
                    }
               }
          }while(swapped);

          System.out.println(“Sorted”);

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestVI
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               int[] arr = new int[10000];

               //fill array
               for(int i=0;i                {
                    arr[i] = 10000-i;
               }

               bool swapped;

               do
               {
                    swapped = false;

                    for(int i=0;i                     {
                         if(arr[i] > arr[i+1])
                         {
                              int temp = arr[i+1];
                              arr[i+1] = arr[i];
                              arr[i] = temp;

                              swapped = true;
                         }
                    }
               }while(swapped);

               Console.WriteLine(“Sorted”);

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 6.3MiB 2.2MiB
CPU Usage: 6% 12%

All Results

Java C#
Test I Memory Usage: 5.6MiB 2.2MiB
Test I CPU Usage: 0% 0%
Test II Memory Usage: 5.6MiB 2.1MiB
Test II CPU Usage: 0% 0%
Test III Memory Usage: 5.9MiB 2.4MiB
Test III CPU Usage: 0% 0%
Test IV Memory Usage: 5.6MiB 2.2MiB
Test IV CPU Usage: 0% 0%
Test V Memory Usage: 10.1MiB 2.6MiB
Test V CPU Usage: 8% 2%
Test VI Memory Usage: 6.3MiB 2.2MiB
Test VI CPU Usage: 6% 12%

Conclusions

Well there you have it. It seems that at this point in time Mono is not only mature enough to compete against a seasoned veteran programming language like Java, but in some cases good enough to even supersede it. This is wonderful news for people looking for alternative cross-platform high level languages.

Some of you may remember an old Windows program of mine called Hash Verifier. It was a graphical utility that allowed people to generate hashes of their files, and then compare those to known hashes, ensuring that their files had not been corrupted. Well in recent months my foray into the world of Linux has finally taken me into the realm of programming on that platform. Being primarily a .NET developer on Windows I have found the Mono project on Linux to be an absolute breath of fresh air.

“Monkey” project

The Mono project is an open source implementation of Microsoft’s .NET common language runtime and a C# compiler. On Linux the easiest way to program in a Mono language is within the project’s own integrated development environment called MonoDevelop.

C is a sharp language

C# is a very powerful programming language that falls somewhere between C and Java in terms of syntax. While my experience with C# has been limited in the past, I was easily able to pick it up quickly thanks to my background in both C and Java, as well as fellow .NET language Visual Basic.

The challenge

Digging up an old .NET project of mine, Hash Verifier, I decided to challenge myself to port the application to Mono. In order to do this I needed to accomplish the following:

  • The original application ran on Microsoft’s .NET on the Windows platform. The new application must run on both .NET on Windows and Mono on supported platforms.
  • The original application was written in Visual Basic. The new application must be written in C#.
  • The original application has a GUI powered by the native Windows.Forms. The new application needs to have a GUI that works in a similar way on all platforms.
  • The new application must be able to fully re-create all of the old application’s features and functions.

Porting = easy

I must say that porting this old application to C#/Mono was a relatively straightforward task. Although I had plenty of GUI toolkits to choose from I ended up sticking with the existing Windows.Forms. Once I had decided on using Windows.Forms as the basis for my GUI (WinForms is a free and open source implementation for non-Windows users!) I set out to create my new application. I was literally able to open the old Visual Basic GUI designer file, copy the code into my Mono workspace, change the syntax to C# and voila it worked!

In fact the only tricky part was trying to figure out a compatibility issue that .NET/Mono 2.0 seem to have with the new Windows Presentation Foundation (WPF). I’ll save you the gory details but basically drag and drop functionality would not work. I eventually rectified this issue by including a compiler flag telling .NET/Mono to execute the form in single thread apartments mode. You can see where I did this in my code by looking right above my static main function:

[STAThreadAttribute]
public static void Main()
{

}

Final result

With the application complete I must say I am impressed. Crafting and running applications for Mono is extraordinarily simple to do, seems very powerful, and the application itself only takes up a couple of MiB to run. In the future I definitely plan on doing more of this type of development now that I am using different operating systems every day.

Hash Verifier

If you are still using the old version of Hash Verifier, or if you would just like to try it out you can download the new Hash Verifier in two different ways. The package marked binary only contains just the program itself and the relevant documentation. The package marked all contains both the program, documentation as well as the source code.

Requirements:

  • All platforms: .NET 2.0+ / Mono, a graphical display
  • *nix platforms: WinForms (identified as System.Windows.Forms)
Binary Only Package All Package
File name: hash_verifier_0_1_0_0_binary.zip hash_verifier_0_1_0_0_all.zip
File hashes: Download Here
GPG signature: Download Here Download Here
License: (LGPL) View Here
Version: 0.1.0.0
File download: Download Here Download Here