Wednesday, September 28, 2011

Difference between Objective C and C#.NET - I

               I am a newbie to Mac. I have seen people saying that learning a new programming language each year is worthy to enrich programming skills .As a beginner to the IT field I feel interesting to explore new things . I would like to share the knowledge I am gaining in finding out the difference between C#.NET and Objective C Programming languages through my blogs so that this will be helpful for any one moving from C# to Objective C.
A direct comparison of both the language seams to be awkward as both are built to use different platform, different frame work, different development environment. Objective C uses Cocoa and Cocoa touch frameworks where as C# language is built to use .NET frame work. Both are rich frameworks in their own purpose and rights as Cocoa and Cocoa touch targets the Mac OS X & iPhone,iPad respectively and .NET obviously the Windows. Similar case with the IDE also, Xcode is the common IDE used for developing Mac application. And Visual studio the usual IDE used for coding C#.NET.
       To begin with syntactically Objective C which is advanced form of C by adding object orientation to it. Where as C# I would say derived from C++ and JAVA. The major difference in both these language comes in fact that C# has a dot notation to access methods. Objective C talks within objects by passing messages.  C# releases the head ache of separating the declaration from implementation of classes and methods when compared to Objective C. Unlike C#, in Objective C the classes and methods are declared at one end and implemented at other end. I would like to introduce the way how Objective C differs syntactically for declaring class and methods.
For any xcode project using objective C two files are created by default one in where the declaration is done called .h file and .m where the implementation take place.To declare and implement a class and a method in C#

public class addition
public void add(int a,int b)
//codes for operation....

where as in Objective C the code is 
@interface addition
//variable declaration
-void add:(int)a:(int)b;
@implementation addition
-void add:(int)a:(int)b;
//codes for operation......

@interface is the keyword used to declare a class and not similar to interface in c# .If you are looking for the interface equivalent of C# in Objective C then it is '@protocol' in Objective C. Any class started must end with an '@end' as shown above. The function 'add' is declared in the class with two parameters separated by the colon . The implementation is done in .m file .

To call a function in C# we go for
addition addobject = new addition();

This could be accomplished in Objective C through the following codes
addition *addobject = [[addition alloc]init];
[addobject add:5:2];
           Another interesting thing that I noticed was the string syntax in objective C and C#. I have used regular expression in C#.NET where I used @ symbol with strings in double quotes to denote quoted string literals. Where as in Objective C it denote the CoreFoundation string .
Another major fact for any person new to Objective C is the memory management. C# provides automatic garbage collection and these has to be done manually in IOS. (ie) every call to alloc should be matched with end of current scope.I would also like to explain the concept of memory management in my up coming blogs..
Rashid Khaleefa..


Anonymous said...

thats an awesome one

sandeepbvnr said...

good one rashid! keep going

Paul Laskin said...


Good write up. A couple of things though. You are right Obj-C is C extended with object capability. But a better way to look at it is C + SmallTalk. It is the message passing component in addition to objects that bring power to Obj-C.

Obj-C does have auto garbage collection (GC) functionality. However, the iOS framework does not include GC. In iOS5, (ARC) Automatic Reference Counting was made available. ARC is a compiler directive that manages retain and release. GC on the other hand occurs during runtime. The end result is similar - memory management that can occur automatically if one wants.

Finally, there is a dot syntax in Obj-C. Has been since Version 2. Some developers love it some think it makes reading code more difficult then the bracket message syntax. Apple sample code uses it. I ... well like brackets.

JohnCornor said...

Hmm is anyone else experiencing problems with the pictures on this blog loading? I'm trying to determine if its a problem on my end or if it's the blog. Any feedback would be greatly appreciated.
data recovery buffalo