๐Ÿ“ฆ about ๐Ÿงป posts

I always enjoyed coding in c++. But coding in C# makes coding in c++ feel like coding in assembly. Here’s 9 reasons why I love it.

No Headers

It’s not until you stop using headers until you realise how stupidly archaic they are.

Var

Using var means you don’t have to actually declare the type. The compiler can work that shit out for itself! This is one of those things that you don’t appreciate when you hear about it.. but when you start using it you really do.

// c++
int myvariable = GetHealth();

// c#
var myvariable = GetHealth();

You can still use int in the c# version if you like, and I’m sure there’s purists that say that you shouldn’t even use var for some reason.

LINQ

I haven’t even scratched the surface of knowing what Linq is all about.. but I know this is cool. Imagine you need to find a player by playerID in a list of players. I appreciate that you’d have probably wrapped all your ugly c++ with macro’s or you’ll be using some super complicated Boost library in c++.. but for arguments sake

// c++
MyPlayer player = null;
for (std::list::const_iterator it = playerList.begin(), end = playerList.end(); it!= end; ++it )
{
if ( (*iterator)->PlayerID == FindID )
{
player = *iterator;
break;
}
}

// c#
var player = PlayerList.Find( x => x.PlayerID == FindID );

Yep that’s right. It’s like writing an inline function. It feels like cheating.


var notPlayers = PlayerList.FindAll( x => x.PlayerID != FindID );
var banPlayer = PlayerList.Find( x => x.Username!= BanName );

Attributes

If you’ve ever written a class factory in C++ you will appreciate attributes. So you make this function..


public class ConVar: Attribute
{
public string help = "";

public ConVar( string help )
{
this.help = help;
}
}

Now in any class in your code you can do this..


[ConVar( "Start health of player" )]
static public float health = 4.5f;

[ConVar( "Player's name" )]
static public string name = "Unnammed";

Now on startup you can make your program find every ConVar attribute and set them up in a dictionary by field name. Then you can add functions to get and change their values. You just made a convar system.

It’s kind of an excited feature once you start digging into it, there are tons of possible uses.

No Macros, templates

Like headers, a lot of c#’s strengths come from the things it doesn’t provide. It’s simple and easy. It doesn’t encourage you to change the language. Sometimes coding in c++ is like writing your own language for things and fooling the compiler into working for you.

Named Arguments

You ever had a function with tons of arguments, or even worse – a ton of optional arguments?


int MyFunction( string shirt = "", string shoes = "", int numEyes = 2, float time = 5.0f )
{
// Do Something
}

MyFunction( shoes: "Trainers" );
MyFunction( time : 2.0f, shirt: "white" );

It’s another one of those things that seems useless, but is really nice.

Interface, Abstract

In C++ you can kind of make an interface but it’s never really proper. With Interface and Abtsract classes in c# you know exactly where you are.

64 Bit

Converting c++ code to work in 64bit is a bit of a mine field. You might get it compiling.. but you never really know whether your code is actually going to be doing bad things unless you have either designed it from scratch for 64bit, or you have gone line by line and verified.

In C# chances are your code is going to just work. Unless you’re doing some really really specific things. To me this sums up C#. It shows how abandoned and old fashioned c++ is.

Mono

Thanks to Mono all this stuff isn’t bound to Windows. It kind of makes your investment in learning c# less useless.

Summary

C++ is never going to go away. It’s probably always going to be the bedrock of everything. But maybe it’s time to re-evaluate whether everything should be coded in C++ by default. Maybe it’s time to think about the future. Writing less error-prone, more future-proof code.

question_answer

Add a Comment

An error has occurred. This application may no longer respond until reloaded. Reload ๐Ÿ—™