Is C# easier to learn than C++?

Recently, it was announced that Thrive might be ported to Godot engine. As you know, the main language to be used in Godot will be C#. I heard some people (outside the forums) saying that C# is easier to learn than C++, and I personally find it hard to believe. It’s especially because I don’t understand object oriented programming overall and I was used to go from website to website to learn how it works; in vain however.
Is it just me or is C# really easier to learn than C++?

Sorry if it’s in the wrong category.

i have never used c++ but i do use c# before and i think c# should be easier as it is higher-level programming language and it is safer to program because of the error checking and a automatic garbage collector

also both languages are object oriented
(object oriented programming is basically programs contain object which have data and function(thing that do stuff) and you can manipulate objects and the data in them) you don’t really need to understand object oriented programming to start learning c#

Except I don’t know what I’m doing in C#, even if the program works. I hate it when I don’t understand something or when I don’t know what I’m doing.
Even though C++ includes OOP, most tutorials don’t begin with OOP features because it’s probably just too confusing.

I agree that C# is easier than C++ because it doesn’t have manual memory management, the garbage collector takes care of deleting unreferenced objects. So you don’t need to understand how computers work on as detailed level to write good C# code. However I just very recently discovered that you can’t easily make const references to objects, so a big C# project needs to be careful to not have one random method in it that messed up the state of some important object.

So overall C# is more beginner friendly. And OOP is the major programming paradigm currently, most if not all popular languages are OOP or are used in a way to simulate it (there are many OOP frameworks for C for example).

Ah this tutorial is much better!

https://www.w3schools.com/cs/default.asp

W3Schools recently started to make tutorials on languages other than web programming.

What do you think about this tutorial, @hhyyrylainen?

1 Like

I guess it’s better than nothing, but it seems to heavily assume that anyone reading the tutorial is already familiar with programming as it just starts throwing a bunch of terms at you without explaining them.
I would stay away from w3schools whenever possible, their individual pages for some specific feature or function are pretty good, but as a tutorial, I think they are quite bad.

1 Like

Well, I find it more interactive than other sites I know.

To all the Godot contributers
@hhyyrylainen
Check this out

1 Like

I’ve seen that before (or at least the part where you can use rider with Godot). And now that you mentioned it, MonoDevelop debugger has been bugging me a bit as it doesn’t properly show the values on mouse hover and I’ve had it crash multiple times while trying to display values.
Though, I’m not sure if I want to spend 139 euros on that.

It’s free for Open source projects. ( you can confirm that part)

I don’t think we actually qualify

as we have plans for receiving money and paying core developers. So I’d actually would have to pay out of pocket for it, though I might try the trial again if they let me, and if it is nice enough buy it.

Sure, give it a try. Currently core developers aren’t being paid. So we are eligible for at least a year.

Ok, I’m going to share my C# learning, which I’m restarting from scratch. That way, more experienced people might give useful advice or maybe just criticize the source, which is tutorialspoint by the way.

https://www.tutorialspoint.com/csharp/index.htm

Environment

C# is based on the .Net framework from MS and people use VS or Mono as an IDE. I’ll just stick with Notepad++ and use cmd for compiling.

csc programname.cs
Apparently, it’s recommended to name a file after its class. Leads to further questioning in my head.

Program Structure

using System; // includes the System namespace

namespace App
{
   class HelloWorld
   {
      
      static void Main(string[] args) // Main = program entry point
      {
         // Single-line
         /* multi-line */
         Console.WriteLine("Hello World!");
         Console.ReadKey(); // prevents autoclose
      }
   }
}

Questions!

  1. What is the string[] args thing?
  2. If a file has a namespace instead of a single class, do I name the file after its namespace instead?

Basic Syntax

using System;

namespace RectangleApplication
{
   
   class Rectangle
   {
      // fields
      int length, width;
      
      public void AcceptDetails() //method
      {
         length = 4;
         width = 5;
      }
      
      public int GetArea()
      {
         return length * width; 
      }
   }
   
   class ExecuteRectangle
   {
      static void Main()
      {
         Rectangle r = new Rectangle();
         r.AcceptDetails();
         Console.WriteLine(r.GetArea());
         Console.ReadKey();
      }
   }
}
  1. Now, do I name the file rectangle.cs or program.cs, hmmm?

Ok, so a class is just a template. Perfect. So I need to create an object from it like so:
Class obj = new Class();

And to access a method or a field:
obj.fieldName;
obj.Method();

And a namespace is a collection of classes.

To be continued…

I don’t know C#, but I’m pretty sure args contains the command line arguments.
Let’s say you wanted to list the contents of directory my_dir like this:

ls my_dir

or (in windows if i’m not wrong):

dir my_dir

ls (or dir) will be run. But how does it know what directory it should read? It will read args to see what command line arguments it received. It sees “my_dir” and then knows what directory it should list. This applies to other programs that read command line arguments.
Edit: Maybe this answer will explain it better: c# - What is "string[] args" in Main class for? - Stack Overflow

2 Likes

not a expert but

  1. Elrakrez already explained above
  2. The name of the file really does not matter, but you should name it after the main class
  3. You would ideally separate it into 2 files but i think in this case it would be called rectangle.cs

also brackey got a good video tutorials if like those:

I guess that’s a fine enough starting point, though as an expert reading through the Tutorialspoint tutorials I’ve found multiple errors. With that C# tutorial, I was able to find a mistake in just a few minutes. They incorrectly talk about destructors running when things go out of scope, which is entirely wrong. Finalizers (as they are properly called in garbage collected languages), actually only run when the garbage collector notices that the object is unreferenced and should be deleted. Microsoft’s documentation says that: Finalizers - C# Programming Guide - C# | Microsoft Learn And one final point is that the Dispose pattern should basically always be used over finalizers.

I guess if you can work through the entire tutorial, it will be a good starting point, even with the mistakes, which you can then build on.

It’s much better for organization to name files after what classes they contain. For Thrive we are using stylecop which will not let your code pass if you don’t name your files after the classes they contain. This is why you should really use an IDE for C# as it can automatically add new files to the project definitions for it to be compiled.

They are the command line parameters passed to your program. For example git pull master would receive the pull master part as args (and possibly the exe name that is running, though I haven’t checked the C# documentation so I don’t know, at least in C++ the first argument is always the running executable name, then followed by the command line parameters).

No, because namespaces are supposed to span multiple files. So if you have a folder called Utilities with multiple files in it, all of them should define their classes in the Utilities namespace. Once again this is a good naming practice to organize large programs.

Files should not usually contain multiple classes. Though, I make exceptions to this rule for closely related classes like having a small interface and the implementation in the same file. But in this case you should have Rectangle.cs and Program.cs separately. And the class containing Main should be named Program.

1 Like

Data Types

Three branches: Value, Reference and Pointer types.

Values types are assigned a value directly.

type Type Size (in bit) Signed? Number of decimals Range Default Value
bool Boolean 8 true or false false
char Unicode character 16 [U+0000, U+FFFF] ‘\0’
byte Integer 8 [0, 255] 0
sbyte Integer 8 [-128, 127] 0
ushort Integer 16 [0, 65.5k] 0
short Integer 16 [-32.7k, 32.7k] 0
uint Integer 32 [0, 4.29B] 0
int Integer 32 [-2.1B, 2.1B] 0
ulong Integer 64 [0, 18.4qi] 0
long Integer 64 [-9.2qi, 9.2qi] 0L
float Floating point 32 6-9 [1.5 \times 10-45, 3.4 \times 1038] 0.0F
double Floating point 64 15-17 [5.0 \times 10-324, 1.7 \times 10308] 0.0D
decimal Floating point 128 28-29 [1.0 \times 10-28, 7.9 \times 1028] 0.0M

Apparently, float and double are binary coded and decimal is not.

It’s possible to determine the size of a type with the sizeof() method, which outputs the answer in bytes.

Console.WriteLine(sizeof(byte)); // outputs 1

Reference types refer to a memory location and come in three built-in types : object, dynamic and string.

Object types can be assigned any value just like dynamic types, except that the former has type checking at compile time, and the latter at run time.

object obj;
obj = 100;
dynamic dyn = 15;

String type can be assigned string values.

String str "Hello";
@"World";

And pointer types refer to a memory location. All those types don’t seem useful for now, given how little explanation they provide. By the way, aren’t pointers a reference type?

Type Conversion

Two ways: Implicit and Explicit.

Implicit conversion converts a smaller type to a greater one. Rules:

  • OK Integer ----> Floating point
  • NOT OK Integer <------ Floating point
  • NOT OK float/double -------> decimal (binary, not binary eh)
  • NOT OK signed Integer --------> unsigned Integer
  • Can’t convert to the same size (byte can’t convert to sbyte)
  • Can only convert if within the right range

Explicit conversion works with methods. Name convention for methods: ToType(). However, to convert to float, one needs to use the ToSingle() method. For integers (except bytes), just use ToInt<size in bit>for the corresponding signed integer type or ToUInt<size>if unsigned.

int a = 20;
a.ToString();

Variables

Eh, variables just store a value within a memory location.

int a, b; OK
int a, b = 5, 10; NOT OK

You can also accept user input using the ReadLine() method.

int a = Convert.ToInt32(Console.ReadLine());

1 Like

No, C# actually has real pointers, like C++, to raw memory. Because they are unsafe and you can crash your program with them, you can only use them inside unsafe blocks.

Not much to add but I appreciate your dedication by making a real table

how do you even do that?

|Column A|Column B|Column C|
|---|---|---|
|a|b|c|
1 Like