C# program in need of critique - will continuously update for critique
36 replies, posted
Some tips and tricks from my long experience with C# so far; not going to critique personal style and how you name your variables as that's entirely irrelevant and subjective in the end, and your binary works exactly the same in the end..
(VS Shortcuts)
In Tools -> Options -> Text Editor -> C# you can set up your default preferences and formatting options
https://files.facepunch.com/forum/upload/108652/a5b1f4cf-9ac6-4a33-9666-c181f5b9e6ec/image.png
(The ones starting with K are C# key bindings)
Ctrl + E, D (Ctrl + K, D) formats entire document
Ctrl + E, F (Ctrl + K, F) formats the current selection
You can create your own snippets for common code patterns you use, here's an example of mine
http://carp.cf/$/0UR3Ri.png
Creates a property that's backed up by a field and sets a Dirty variable every time you change it (if the values are different). Here's the source for that.
You include it with Tools -> Code snippets manager, there are also some editors which have a nice GUI instead of having to manually type XML.
(This is the point i realize you wrote VS Code and not VS and now i don't want to delete this wall of text i wrote)
C# has great support for calling native code from C# and C# from native code. I feel the need to mention this because not a lot of people know about it. You can directly invoke a .NET binary from C. Sadly not a language feature just yet, but it's supported by the IL. DllExport on GitHub.
P/Invoke is a powerful tool; so some tips for that
You can set the calling convention on delegates
Do not include the extension of the library that you are invoking code from, this makes it work by default across all platform (.dll, .so, .dylib, whatever) without custom config file for that
You need to pin delegates before passing them to the unmanaged side if you don't want the garbage collector to collect them.
You can create custom marshal classes which handle objects before passing them between the two sides
I dislike overusing of properties, especially if you don't need to do logic in get/set or have separate access permissions for get/set. If you're writing a library and are afraid to break backwards compatibility, you should probably use properties if you ever plan on doing logic. As replacing the one with the other will break applications using your library and they'll need a recompile.
Just a heads up: that second image you posted is being blocked by nod:
https://files.facepunch.com/forum/upload/111343/5a81a5fa-0173-4ac4-9ca9-4761b8cad92e/image.png
I don't get it. Nod32 is pretty good.
These blocks are usually because image hosts forget to check user uploads for hidden files contained in images, which sometimes are payloads for viruses.
I suggest this
https://i.imgur.com/xgf6wYL.png
Please don't hijack my thread with an anti-virus debate
Well... disable nod then.
Sorry, you need to Log In to post a reply to this thread.