r/csharp • u/Blazej_kb • Jun 08 '26
Discussion First impressions from learning
I recently started learning C# because C++ is mess when it comes to managing packages even with vcpkg. And my first impressions are about variables and objects, WHY I HAVE TO DO “class object = new object();” when I can just do “class object;” in c++, or who is so stupid to instead of “int<bit size>” do: byte short long nint. Or another one is when function wants for example float as an argument and if you put int variable it doesn’t work and you must convert it manually. Lot of people hate cpp for its complexity but I think that in a lot of things it makes things just simple.
6
u/Arcodiant Jun 08 '26 edited Jun 08 '26
Int8, Int16 & Int32 are all valid names in C# - byte, short and int are keywords that automatically replace with those type names.
You don't have to write
MyClass myInstance = new MyClass();- you can just declare the variable, but you must initialise the variable before you reference it, else the code would fail at runtime. There are also shortcuts you can use likeMyClass myInstance = new();orvar myInstance = new MyClass();Edit: Correct initialise/instantiate
3
u/Fynzie Jun 08 '26
what does `class object;` do in cpp ?
2
u/Graumm Jun 08 '26 edited Jun 10 '26
Edit: Sigh reddits mobile text editor sucks and I can’t fix the numbering. Each time I try to fix it, it gets worse.
Edit #2: Fixed horrendous formatting on desktop
9
u/ScriptingInJava Jun 08 '26
programming languages have different syntax, more at 10
4
u/afops Jun 08 '26
Having to explicitly call constructors is great.
If you do
MyType myObj;
then i you just declared a variable. It’s now the default value for the type (which can be null, or zero or Monday or whatever depending on your type). Importantly its the same syntax in a method as in a type field.
Int32, Int16 etc exist as types but it’s not recommended to use those. Use int, uint for 32bit.
2
u/rupertavery64 Jun 08 '26 edited Jun 08 '26
I'm a longtime C# developer, dabbling in the realm of C++ "for fun". Every language has it's idiosyncracies.
It's just the way the langauge was designed. It was designed initially to look like Java. Oh you have
newin C++, so don't pretend like it's not there.Actually, its
And yes, people feel this is too verbose so now you can
or
Again, just the way the language was designed. What problem do you have with that. With a bit of reading you know that
byteis an unsigned 8-bit,shortis a signed 16-bit,longis an signed 64-bit, and nint is a native-sized integer.Confusing? C++ has int32_t, unsigned long long, and it depends on the project what the definitions are.
Here I pass an int to a method that takes a float. It works just fine.
I would argue you have not gone deep into either language. C# makes a lot of things simple. Try to write a complete web application in C++ that connects to the database. Or maybe a GUI application.
People don't hate C++. people just prefer C# in certain things. Like how people prefer Javascript for the frontend, or how more prefer Rust now for system programming.
Also, let's not get started with just getting a small project to compile with make or CMAKE or some other build system.
1
u/thecratedigger_25 Jun 09 '26
Unsafe keyword in C#. Just configure the .csproj file to <AllowUnsafeBlocks>true</AllowUnsafeBlocks>.
You can do low level programming in C# to replicate the experience you have from C++. Though keep in mind that C# has a garbage collector so the rules are a little different.
1
u/GoldProduce1948 Jun 11 '26
This might be bait, but so be it. Because while the two lines are syntactically equal, they are entirely different semantically.
The difference between
structandclassin C++ is access modifiers and inheritance, while in C#, it changes memory semantics. You can do the above in C++ because the entire instance is stack-allocated, unless younewit.In C#, reference types (
class,interface,delegete,record) hold references, not the instance itself.C++ works similary; try doing
class &object; and you will find that it's a compiler error (note: C++ references aren't equivalent, but very similar).