this post was submitted on 13 Nov 2024
1728 points (96.1% liked)
Technology
59627 readers
3684 users here now
This is a most excellent place for technology news and articles.
Our Rules
- Follow the lemmy.world rules.
- Only tech related content.
- Be excellent to each another!
- Mod approved content bots can post up to 10 articles per day.
- Threads asking for personal tech support may be deleted.
- Politics threads may be removed.
- No memes allowed as posts, OK to post as comments.
- Only approved bots from the list below, to ask if your bot can be added please contact us.
- Check for duplicates before posting, duplicates may be removed
Approved Bots
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Sure you can, it’s the same in C# as it would be in C++ if you did a=b, where a and b are both pointers.
You don’t want to copy the full data of a class around every time you use it, that would have extremely poor performance. If you do want that behaviour, use structs instead of classes. If you need to clone a class for whatever reason, you can do that too, but it’s not really something that you should need to do all that often.
I don’t think you should really jump in and call something crappy if you just don’t really know how to use it, personally!
In C++ you have the choice, the compiler makes the shallow copy (you know what that is right?) automatically if needed, or you can move around the pointer or a ref. Or, transform the shallow copy into a deep copy if you need that.
In c# you don't even not have the choice, ints etc gets copied but classes aren't. Where's the logic behind that?
And as so many others you scramble to find some excuse that "you should probably not do that very often anyways" or some other bullshit.
I heard all that 20 years ago when it actually had some merit for people trying to run it on the old crappy hardware of the day, today it's just moot.
Need speed or low memory usage? Learn to code in C/C++ for example. Heard Rust is great too.
C# is just an old wonky language.
/Rant off
Just for context, I’m an experienced software engineer with years of experience with both C++ and C#, as well as several others, including Rust. You can do shallow and deep copies in C# as well, it’s done extremely infrequently because it’s usually a bit of a code smell and it has some downsides - it’s inefficient both for performance and for memory.
In C# the assignment operator will copy the value if it’s a value type (structs and primitives) and copy the reference if it’s a reference type (classes). It does that because it’s a garbage collected language and it needs to track how memory is referenced and so on.
The whole debate about what languages are better is honestly a bit silly, IMO. C, C++, Python, C#, Javascript, Rust, they all serve their purpose, they have their strengths and weaknesses.
Now you are doing it again, you just say generic sensible stuff strawman way.
Like yeah I know about "the assignment operator" and it's still shitty you can't decide what it's copying. You explaining how it works doesn't make it better lol!
You are also blatantly wrong, there is absolutely no reason to not let you copy a class or pass an int as a ref "because of the garbage collector", if you want to make a language having these functionalities there is nothing preventing it to be done correctly.
Also c# is an inefficient language to begin with, not letting the user do as he pleases is just dumbing things down, nothing to do with efficiency.
Also, we all have tons of experience (5 years xp isn't experienced btw) no need to go get your diploma :-)
And another straw man, when did I say that a language is better than another? Never did I do that, but I guess you are not happy with all your arguments geting shot down.
C# is a crap language, if you are forced to use it like at work, or if you don't want to learn another one, use it!
But you can't polish a turd.
Sorry I insulted your favourite programming language lol.
The truth hurts the most.
"deleted" lmao what did the comment say? OP deleted it after all it seems.