Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

Why a C++ Developer May Find it Uneasy to Work With C#

Rate me:
Please Sign up or sign in to vote.
1.33/5 (6 votes)
22 Mar 2012CPOL4 min read 21.4K   1   22
How C# is highly distinct from C++

Let me put up a disclaimer first, since the title of the article may contradict with the views of the C# developer community. This article is based on my own experience working on both C++ and C# languages for more than 4 years. And this article is intended for developers migrating from any flavors of C++ technology to C#. Also, a C# developer may look upon this article to get a picture of how distinct and abstract C# language design is when compared to other programming languages which run natively on hardware.

This article is highly abstract, which looks only into syntax and semantics of the languages, and doesn't really dig deeper into design considerations of the language.

So, let's quickly get to the point. How is C# distinct from C++?

Image 1

One of the prime goals of C# language design is to get rid of ugly pointers and make coding easy, quick, clean which finally conduces "Rapid Application Development". But believe me, a C++ developer feels at home working with pointers, it's no real fun coding in the absence of pointers. In fact, a matured C++ developer finds it really hard to write programs deprived of pointers.

Not to be demotivated, Microsoft bequeathed every single responsibility of 'pointers' in the hands of " References " which basically alludes pointers. The message is clear... Microsoft doesn't expect or allow developers to mess around by having direct memory access.

The .NET Framework further makes developers' life easy or rather I say lazy by, completely managing the memory allocations and deallocations in the name of "Garbage Collection" and branding the resultant code as " Managed Code". What I really don't understand is that when an experienced developer can write complex algorithms, can design systems involving multilevel difficulties, is it really hard be responsible for memory management.

By delegating memory management to framework, a developer really loses flexibility on design and functioning of the system. If a developer had the luxury of memory management as in C++ he could clearly circumscribe on the target hardware capabilities and limitations on which the program will be run. And could also customize applications to be run on platforms constrained by hardware limitations.

The basic principle of Object Oriented Programming conveys that "Class represents a generic blueprint of an instance and values of data members are specific to an instance, but 'static' is an exemption here. Contrary to this, C# language design allows developers to assign initial value to data members of a class as shown below.

C#
public class Student
{
   public string Name = " James Frank " ; 
}

ANSI standards for Object Oriented language implementation ensures initialization of data members of a class to default values through default constructor. If we already have a default constructor which is clean, and serving the main purpose, C# feature of member value assignment inside class definition is "Redundant" and affects code readability. Any feature of a language unless it serves a signification purpose is redundant or burden.

C# developers must be aware of the fact that it is possible to explicitly convert a value type variable to reference type, and a reference type variable to value type through a feature called Boxing and Unboxing respectively.

Boxing

C#
short S = 20 ;
object ShortObject  = S ;

Unboxing

C#
short S = (short) ShortObject ;

C# provided this feature to have a unified type system in place which allows value type to represent the underlying data altogether wrapped in a new representation called " reference " and vice versa. Unless there is a flaw in design, how the polymorphic classes are built this feature is of no worth, and if at all one is using this feature, it necessitates the need to trace back derivation hierarchy of polymorphic classes.

This is an odd feature of C#, it breaks the basic understandings of Object Oriented Programming in being developer friendly. Below is an example of static constructor:

C#
public class employer
{
      private static string CompanyName ;
      static employer()
     {
         CompanyName = "Nokia" ;
     }
}

Static constructor ensures that static data members are always assigned to an initial value. And it is called, when an instance of class is created or a static member function is invoked.

I mentioned this is an odd feature because, this is a workaround for C# language design which doesn't allow anything to be defined outside of a class. In the absence of this constraint, static data members could have been defined and initialized outside class definition as in C++.

C#
string employer :: CompanyName = "Nokia" ;

C# has introduced couple of enhancements in parameter passing mechanisms, to cut short developer efforts from writing lengthy code. Out of which out and ref are the two parameter decorators. Value for out parameter is assigned by the called member, as shown below:

C#
public static void Add ( int x, int y, out int ans )
{
     ans = x + y ;
}

Usage

C#
public static void calculate()
{ 
     int ans ;
     add ( 1, 2, ans ) ;
}

The sum is stored in ans variable by callee add.

Now consider the ref decorator, the only difference is ref parameter must be initialized before being sent to callee. So if this is the only constraint we don't really need out parameter at all, all which is required is to use ref, ensuring that the variable is assigned to initial value.

  • Absence of Pointers
  • Memory Management
  • Member Value Assignment Inside Class Definition
  • Boxing and Un-Boxing
  • Static Constructors
  • Redundant Out and Reference Parameters

These are a couple of features of C# which I found redundant, ugly, which lead to confusion, and mystify the clarity of the language rather than assisting a developer. However, these are my own views, but not conclusions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
India India
I am Vijay hailing from Bangalore, the silicon valley of India. I posses extensive expertise working in mobility domain. I have a natural propensity for "Research and Innovation".

My areas of interest include Pervasive computing, Cognitive sensors, HCI, and Multimedia. Being strongly resolute with an everlasting fervor for learning, over the years I have learnt to come out of comfort zone to experiment with state of the art technologies and to contribute to developer community.

I blog about innovations and latest technologies from mobility domain, about development environments and programming languages across different platforms.

Blog : Amusement of a speculative mind...[^]
Projects : Amusement of a dilettante mind...[^]
LinkedIn : Vijay D R [^]

Comments and Discussions

 
QuestionI will not vote, but... Pin
Paulo Zemek30-Mar-12 3:40
mvaPaulo Zemek30-Mar-12 3:40 
AnswerRe: I will not vote, but... Pin
JackDingler6-Apr-12 10:50
JackDingler6-Apr-12 10:50 
GeneralRe: I will not vote, but... Pin
Paulo Zemek6-Apr-12 10:52
mvaPaulo Zemek6-Apr-12 10:52 
GeneralRe: I will not vote, but... Pin
JackDingler6-Apr-12 11:05
JackDingler6-Apr-12 11:05 
GeneralRe: I will not vote, but... Pin
Paulo Zemek6-Apr-12 13:55
mvaPaulo Zemek6-Apr-12 13:55 
GeneralRe: I will not vote, but... Pin
JackDingler6-Apr-12 16:38
JackDingler6-Apr-12 16:38 
GeneralRe: I will not vote, but... Pin
Paulo Zemek6-Apr-12 16:42
mvaPaulo Zemek6-Apr-12 16:42 
GeneralMy vote of 1 Pin
johannesnestler28-Mar-12 6:07
johannesnestler28-Mar-12 6:07 
GeneralRe: My vote of 1 Pin
JackDingler6-Apr-12 10:51
JackDingler6-Apr-12 10:51 
GeneralMy vote of 1 Pin
Octopod27-Mar-12 5:24
Octopod27-Mar-12 5:24 
GeneralMy vote of 1 Pin
matt_taws23-Mar-12 7:43
matt_taws23-Mar-12 7:43 
AnswerRe: My vote of 1 Pin
Vijay Rajanna25-Mar-12 7:49
Vijay Rajanna25-Mar-12 7:49 
Hi,
Thanks for your feedback.

Yes I have a true adulation for C++, working nearly for 4 years on C++, I have found no
other programming language as comfortable coding as C++.


Mathäus Gregório Mendel wrote:
you failed to understand the purpose of C#.

I really can't accept this, but I'm not denying either.
I have been working on C# for last 2 years, predominantly for standalone desktop applications. And this article was written based on my own experience working on C# for this last 2 years.
I accept the fact that the the real purpose of C# is just not desktop app development, but it is much beyond, and also I should explore the wide spread of C# in other domains like web, embedded etc..
All I wanted to convey through this article was, to make development process easy, and rapid, C# was introduced with soo many features which rather leads to confusion and helping a developer. However this is subjective, other developers may find all these features worthy.
GeneralMy vote of 1 Pin
stikves22-Mar-12 21:48
stikves22-Mar-12 21:48 
AnswerRe: My vote of 1 Pin
Vijay Rajanna25-Mar-12 8:04
Vijay Rajanna25-Mar-12 8:04 
GeneralMy vote of 1 Pin
snortle22-Mar-12 15:20
snortle22-Mar-12 15:20 
AnswerRe: My vote of 1 Pin
Vijay Rajanna25-Mar-12 8:09
Vijay Rajanna25-Mar-12 8:09 
Question[My vote of 1] What is the use of poor articles like this???? Pin
LLKrisJ22-Mar-12 13:16
LLKrisJ22-Mar-12 13:16 
AnswerRe: [My vote of 1] What is the use of poor articles like this???? Pin
Vijay Rajanna25-Mar-12 8:14
Vijay Rajanna25-Mar-12 8:14 
GeneralMy vote of 3 Pin
Oshtri Deka22-Mar-12 11:53
professionalOshtri Deka22-Mar-12 11:53 
AnswerRe: My vote of 3 Pin
Vijay Rajanna25-Mar-12 8:18
Vijay Rajanna25-Mar-12 8:18 
QuestionI don't miss pointers Pin
Oshtri Deka22-Mar-12 11:40
professionalOshtri Deka22-Mar-12 11:40 
AnswerRe: I don't miss pointers Pin
stikves22-Mar-12 21:51
stikves22-Mar-12 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.