In case anyone wonders about the line using the -> operator: pointer->ToString() is the same as (*pointer).ToString() So, it means, "get the int from pointer and call ToString() on it"
@codemonkey61732 жыл бұрын
and the parens around *pointer are needed due to operator precidence
@pxolqopt35972 жыл бұрын
This is old news for anyone with the smallest amount of experience in C or C++
@znefas2 жыл бұрын
more formally, it's not "getting an int", but rather dereferencing for you
@magnusm4 Жыл бұрын
@@pxolqopt3597 It's news to us who never touched C or C++(whose file type is called cpp) even after seeing C and C++ code several times.
@SimonClarkstone2 жыл бұрын
"checked" is useful in some non-low-level code too, when converting from 64-bit IDs in a database to 32-bit IDs in an API.
@siriusblack99993 жыл бұрын
i've had to use all but one of them, i've never used stackalloc, but since i'm working with C# to C++ interop, these concepts are actually used quite frequently :P
@nickchapsas3 жыл бұрын
Damn I didn't know that people still do C# to C++ interop. That just blows my mind.
@iGexogen3 жыл бұрын
@@nickchapsas I use interop in mobile xamarin projects, we have some library implementing digital signature and encryption in GOST algorhytm, it is implementing Microsoft C++ CryptoApi, only way to use it on mobile is via interop. Only things I use for it is Marshal class and sequental layouted structs.
@siriusblack99993 жыл бұрын
@@nickchapsas well i'm writing a C++ library for unity, so most of these things have at least cropped up at some point, though we do our best to limit it, since it does add a lot of complexity
@themiwi2 жыл бұрын
@@nickchapsas try calling a Win32-API...
@magnusm4 Жыл бұрын
@@nickchapsas Then I'd recommend you look into the Stride Engine. A really buggy and fragile mess but it's mostly 90 - 99% written in C# and some basic bare bones C++.
@bilbobaggins6013 жыл бұрын
I am so glad that i found this channel. It always keeps my attention, and the concepts discussed are usually so intriguing
@twiksify3 жыл бұрын
I've used sizeof a few times, it's useful when implementing low level integrations that require fixed package sizes. When simulating a TCP/IP protocol for instance.
@nielle19633 жыл бұрын
When reading the title, I definitely assumed at you would cover “volatile”. :)
@nickchapsas3 жыл бұрын
Funny enough, volatile is in the part 2 of this video :D
@Adamturner69503 жыл бұрын
Same thing that poped in my head lol
@iGexogen3 жыл бұрын
I don't think volatile is applicable to this topic. I completely agree about all that unsafe stuff, it is almost never needed even if you sometimes work with unmanaged code like me. But volatile is not about low level code, it is about thread-safety in high level code, many people use it because it is more convinient way than explicitly using Interlocked class.
@nickchapsas3 жыл бұрын
@@iGexogen The topic isn't about low level code. It's about keywords that you don't usually see used in C#. I just happened to have grouped many low level stuff in one video to help it be easily consumed.
@iGexogen3 жыл бұрын
@@nickchapsas It will be interesting to see how many of commentators actually use volatile, I think that unity guys must be using advanced multi-threading. It was suprising for me that you never needed it, I suppose lock statements and concurrent collections is enough for all your async purposes. Waiting for part 2)
@chiwai1986nl3 жыл бұрын
When doing image processing, unsafe is the best option for performance.
@lazartrajkovic59112 жыл бұрын
In 'normal' or usual C# (C# only development) majority of these keywords probably wouldn't be used. But if your C# program works with unmanaged code (C++, Fortran...) these keywords are pretty useful. For example sizeof is used for types interoperability. You must be specially careful when mixing managed and unmanaged code. Another great video, Nick!
@barmetler3 жыл бұрын
I want to point out something about pointers in C#: in C#, the star is part of the type. In C++ and C, the star is part of the declarator. What does this mean? In C#, "int* a, b;" will declare two int pointers. In C++, "int* a, b;" will declare an int pointer and an int. This is why we align stars and ampersands (for references, I'm not talking about the adress-of operator) to the right in C++. Because it is part of the declarator: "int *a, b; int *x, *y;" So if someone puts the star on the left in C++, that doesn't mean they have different style taste, it just means they don't understand how it actually works.
@samwhaleIV3 жыл бұрын
Really solid explanation. Thank you.
@BGroothedde2 жыл бұрын
I agree mostly, with the exception of your last remark. I use right-aligned operators too, but I also understand why people use left aligned operators. Especially in businesses where multiple declarations in a single line are forbidden in their coding standard (a lot of companies have this as a rule). I would argue that aligning them to the left can make it a lot more clear to the reader what a variable or member can contain, there is a lot of debate of semantics versus technicality here. Saying someone does not understand how it works because you do not agree with their choices, is not a constructive point. I have had people that designed compilers explain to me why they used left aligned.
@barmetler2 жыл бұрын
@@BGroothedde I guess I was being a little too general here. But 90% of the time, people put it on the left because they don't understand the implications of it. Maybe not in a job setting, but when people make the decision themselves. We can argue about good practice, but that never ends well. My stance on style is just to do what the project you're joining already does, so if a company puts the star on the left, I'll do that too. But if you see a star on the left in a personal project, it's an _indication_ that they lack some understanding of the language. But frankly, I was unaware that some companies do it, because it is imo just incorrect, the star belongs to the declarator. But if you don't ever group declarations (which I also don't do), then it might not matter so much.
@deanjohnson82332 жыл бұрын
Saying the star is not part of the type in c++ is nonsense. If that was the case, std::is_pointer and std::is_pointer should return the same value. Obviously they don’t and there are plenty of other examples where in type contexts whether something is a pointer type or not matters. I get what you are saying about multiple variable declarations, but I would contend that is a defect of the parsing rules of the language. In all other cases, you need to consider pointer/reference as part of the type of the variable.
@barmetler2 жыл бұрын
@@deanjohnson8233 I don't think you understood what I said. Of course it is part of the type whether something is a pointer or not. But in the declaration statement, the star is not part of the token that describes the type, which is proven by the example I supplied. Also, a defect? It would be incredibly easy to "fix" if it was. It's by design.
@IvanRandomDude3 жыл бұрын
Unsafe is C trying to fool us into using it
@unsafecast36363 жыл бұрын
_takes your hand_ join me in the dark side for real though C is addicting, you should try it some time
@Xotchkass2 жыл бұрын
@@unsafecast3636 nah. Lack of normal build system and package manager kills any motivation to write anything more serious than helloworld's in it.
@switchblade62262 жыл бұрын
@@Xotchkass i think youre forgetting about cmake, conan & vcpkg
@klightspeed3 жыл бұрын
2:17 - casting an IntPtr to an int under 64-bit where that int would overflow will also throw an OverflowException, as the conversion occurs in a checked context. Using unchecked will also silence some overflow warnings, e.g. when working with the lparam in WndProc. 5:34 - that's a 64-bit pointer, so 6:39 you probably want to cast to a long, as casting to an integer will chop off the upper 32 bits of the 64-bit pointer. 9:16 - C# strings are not null terminated - the implementation just happens to put zeroes in the slack space after the end of the string.
@tslivede2 жыл бұрын
In the C# language specification, section "22.7 The fixed statement" it says: A char* value produced by fixing a string instance always points to a null-terminated string. Within a fixed statement that obtains a pointer p to a string instance s, the pointer values ranging from p to p + s.Length ‑ 1 represent addresses of the characters in the string, and the pointer value p + s.Length always points to a null character (the character with value '\0').
@superpcstation3 жыл бұрын
I love your videos, but your advanced c# stuff is extra awesome.
@benjaminclehmann3 жыл бұрын
The unsafe examples are potentially useful for interop. Although generally you use IntPtr and Marshalling when possible. Also, fixed lets you use fixed-sized arrays without initializing one. It's very niche but it's the only thing I've actually used fixed for.
@nickchapsas3 жыл бұрын
And it can also be used with struct which I wanted to show but I totally forgot 🤦♂️
@magnusm4 Жыл бұрын
My first thought was interop, though I didn't know this word for it. Otherwise my second was creating your own little compress program. Taking all the variables into an array of pointers. Go through each pointer and look for the same char or int in the same spot. Put that char/int in it's own struct variable along with it's place in the pointer, and remove it from the original variables. Then to zip it up. Just run a function that takes the struct and puts the char/int into the coordinate place given. Can probably do that better using a dictionary but I just thought of a struct for simplicity. Probably a poorly optimized mess of a program that takes more memory than anything but I just liked to think up the concept of a zip compress program yourself by reducing the address values.
@curtisdh42693 жыл бұрын
Minor complaint about the the thumbnail. I'd love to be able to see these commands before I click on the video instead of them being blurred out, it'd peak my interest and give that effect of like "oh what does that keyword do"
@curtisdh42693 жыл бұрын
@@lorinatzberger3624 Imo the clickbait would be more enticing if it wasn't blurred.
@curtisdh42693 жыл бұрын
@@lorinatzberger3624 Fair point
@InterRubke3 жыл бұрын
We do image manipulations in unsafe mode for performance reasons. Best to do this in a seperate project.
@SpicyMelonYT3 жыл бұрын
The sizeof keyword is very useful when dealing with gpu calls. I do this a lot in unity using compute shaders and compute buffers
@cheebadigga40922 жыл бұрын
All of these can be very handy when interfacing with C code (P/Invoke etc.) to reduce heap allocation in C# code when you know what you're doing..
@mirabilis2 жыл бұрын
I've used unsafe code in one of my projects. Used pointers to speed up bitmap calculations.
@willemschipper77363 жыл бұрын
You have such a great channel, I always learn something!
@kenbrady1192 жыл бұрын
My first C# program in 2003 (the one where I 'cut my teeth' on C#, transitioning from C++) was an audio/MIDI app. Interacting with the WIN32 audio subsystem required allocating buffers, pinning them, and passing them via interop into unmanaged waveIn__ routines of the Winmm module. It was "unsafe" as heck, but what to expect from a C++ programmer :)
@SamCarleton3 жыл бұрын
Thanks for sharing, I didn't know about a number of these. They help explain how to interact with unmanaged DLL's without having to resort to a C++/CLI layer, like I have in the days gone by. Thanks again!
@Micz843 жыл бұрын
Unsefe, stackalloc, fixed and sizeof are good to know if you are using C# for Unity and you are using data-oriented technology stack. Stackalloc is good for avoiding false sharing.
@bishop89583 жыл бұрын
I actually learned about Unsafe code because of unity, Though I didn't know about stackalloc.
@Kalisparo3 жыл бұрын
Being able to use unmanaged code can be extremely beneficial when you're dealing with optimisations and having to do a lot of calculations/large data sets. In addition, you should learn how to use weakrefs for much better io performance allowing you to use large amounts of memory better when you would otherwise have a lot of disk io. Server applications are really good for this. Things starts getting strange when you need to move in and out of managed space, like you need to have an unmanaged thread (like in a program written in CPP) suddenly enter managed space and then back out.
@MarkusSchaber3 жыл бұрын
There's also a project level property true which can also be set in the project properties (at least in Visual Studio), tab "build", then "Advanced", it's called "Check for arithmetic overflow". It sets the global default to "checked", then you can use the "unchecked" keyword to opt out. :-) From the language design view, they should have made "checked" the default, but nowadays, it's far too late to change the default, breaking 20 years of code. :-)
@MrAymenmatador3 жыл бұрын
Great video. This won't be weird if you are a C developer. Actually you had to deal with most of those keywords every day
@TheoWerewolf3 жыл бұрын
The sizeof/unmanaged thing seems odd. When you pointed out that sizeof works with structs that didn't surprise me because internally, base types like int and byte ARE structs (except string which is an oddball type). "int" maps to struct Int32 {}... "long" maps to struct Int64{} and so on. Which now has me wondering why they would consider a struct to be "unmanaged". It does make a kind of sense in that structs are "value" objects not reference objects and have to have concrete existence when declared... but I'm not sure how that makes it "unmanaged".
@sonicbhoc3 жыл бұрын
I do embedded systems programming and I can tell you I use all of those keywords with some frequency. Except stackalloc. That is a cool trick I'll have to remember.
@shanehebert3963 жыл бұрын
I've seen some unsafe code in graphics manipulation code. It may come into play if you need to access some types of hardware or something as well (if you're writing your own code and not using a library).
@henke372 жыл бұрын
C sharp generally doesn't run in contexts that have access to memory mapped hardware.
@kleinmike3 жыл бұрын
I like to mod in games. and to know that you can do pointers natively in C# without a 3rd party dependency is great
@TheKataan3 жыл бұрын
Resharper generates unchecked gethashcode by default. So that one I see often. Used all the unsafe features in my serialisation library (NHessian). Fastest way to calculate equality and hashcode for strings (char arrays) as well as decode Unicode.
@KanashimiMusic2 жыл бұрын
About the checked/unchecked keywords: unchecked is USUALLY unnecessary, because unchecked is the default state at runtime. However, CONSTANTS are checked at compile time by default, so if you want to cause an integer overflow with constants, the compiler will throw a compile time error at you and tell you to use the unchecked keyword. So for example, this: const int CONSTA = int.MaxValue; const int CONSTB = CONSTA + 42; will not compile. It will only compile if you write CONSTB = unchecked(CONSTA + 42);
@MichaelMiller-rg6or3 жыл бұрын
Excellent video! I actually didn’t know about checked or stack_alloc.
@xxdeadmonkxx3 жыл бұрын
It is all useful when it comes to create lowlevel graphic api wrappers etc btw "fixed" keyword can be used inside structs to create "embed" arrays inside struct if you really need cache friendly data with arrays in ECS or something
@micmacha2 жыл бұрын
C# is a powerful and, *if you know what you're doing*, a potentially very fast language; but sometimes I worry about the size and inconsistency of the lexicon. We have changes in LINQ, introduction of C syntax, all of these things, which have worked out so far but tend to leave novices feeling out of their depth.
@TheRuko152 жыл бұрын
I've used stackalloc and sizeof once; when generating waveform images of hours long audio files.
@marna_li3 жыл бұрын
One of the main reasons for using these is to write your own .NET bindings to unmanaged code - like P/Invoke to some DLL written in C/++ or even Rust. But nowadays there are tools that can generate that for free.
@BittermanAndy3 жыл бұрын
A tool such as...? I don't need to do it often, but it's always a pain when I do.
@Keldor3143 жыл бұрын
Guess what those tools use under the hood. ;-)
@keithrobertson75793 жыл бұрын
Some other keywords which devs don't use often but should know how to use them: `volatile` for thread interaction, `extern` for defining PInvokes, and `implicit` and `explicit` for type conversion operators.
@BittermanAndy3 жыл бұрын
I've used all except stackalloc. But not often. Apart from unchecked, which is needed for good implementations of GetHashCode (among other things, but that's the common one).
@JohnEBoldt3 жыл бұрын
I thought you were going to include the goto statement, which I have only seen used once (not by me!) The last time I used goto was 40 years ago when I was doing Fortran!
@stephenyork73183 жыл бұрын
Real world example...I once had to index multiple millions of records of electoral role details on a laptop. The machine was disconnected and SSDs weren’t around at the time. To make the thing work in a realistic time it was an in memory solution. When I put everything in memory with a dictionary there simply wasn’t enough RAM for it to work. I ended up implementing a very lean hash table with collision detection using unsafe blocks and we managed to load everything in with room left over.
@nickchapsas3 жыл бұрын
Wow, in C#?
@David-id6jw3 жыл бұрын
I used checked/unsafe/fixed a lot back in.. gosh, .NET 1.1 days, when writing a scientific/image processing application. Obviously no stackalloc because it didn't exist back then, though I have used stackalloc in a small, high performance algorithm recently. Sizeof is the one I find most niche, despite it also being the one that most people would be familiar with. I know an int32 is going to be 32 bits == 4 bytes, so I don't need code to tell me that. But it's also kind of like using nameof(parameter) to get a string representation of a parameter or property or whatever - you don't need it _right now_ , but it makes maintaining it later a little easier.
@killpopers3 жыл бұрын
I've used all of these but only because I have done some low level graphics stuff from C# for intrest rather than any real use but it an example of when you might do such a thing
@staskovalevsky3 жыл бұрын
Thanks for your videos! One of the best .net youtuber
@adamschneider8683 жыл бұрын
That sizeof code example is pretty damn cool
@Flubberia3 жыл бұрын
Cool. Actually we are using sometimes unsafe and stackalloc in our projects where we need really high performance (hot path). Also playing with structures to achive like super performance 0 allocation lock free hype stuff. =)
@JoeBonez3 жыл бұрын
The one time I’ve used unsafe and pointers was using writable bit maps for creating height maps.
@micmacha2 жыл бұрын
As an experienced C programmer these are great. You have to worry about your own pointer safety, but Jesus is it running fast.
@Bomag3 жыл бұрын
Some of these are less often used like checked/unchecked, but these are far from 'definitely never had to use' territory for me personally. It isn't even about performance either, just the constant need to have easy to use tools for PInvoking vendor libraries, system calls, etc. Some of these are still being improved like the stackalloc you mention wasn't possible to use in safe contexts until recently with the addition of Span. I'm glad Microsoft keep improving this space to be honest.
@JohnLudlow3 жыл бұрын
I have had to use sizeof once but it's not exactly common in C#. I'm surprised goto wasn't on the list.
@danielhadad49113 жыл бұрын
What did you use it for?
@MrKulkoski3 жыл бұрын
I actually have a use for the combination of unsafe, unmanaged and sizeof combo in a project I'm doing, lol.
@raznagul3 жыл бұрын
I'm quite suprised by checked/unchecked. I always thought checked was the default.
@phizc3 жыл бұрын
It's checked at compiler time, so I sometimes have to do const int Default = unchecked(0xCA7F00D1); At runtime it is unchecked unless the compiler option CheckForOverflowUnderflow is set.
@GhostTyper2 жыл бұрын
Back in .NET 2, 3, 3.5 and 4 i used pointers all the time, because those versions were sooooo slooooow.
@mk72v2oq3 жыл бұрын
I always use all the unsafe stuff. This is just the way to write real high performance code. And you are VERY underestimating the performance gain. In some cases it can make code hundreds or even thousands times faster. But yeah, this is not common approach for C#. Such things are really useful only when you are familiar with C/ASM level of things.
@nickchapsas3 жыл бұрын
There is no doubt in my mind that coding like this would yield great performance gains but it comes with a lot of shortcomings. Code needs to be easy to maintain, easy to work with and easy to get new hires to work on. As a hiring manager myself, I can guarantee that if I was using all those keywords in our codebase then it would be impossible to hire engineers. And at the end of the day, it is way easier to scale an app out to 100 stateless instances than optimize that single codebase.
@mk72v2oq3 жыл бұрын
@@nickchapsas in general - yes. But sometimes kzbin.info/www/bejne/bXi3oaqdqNJ7nbc
@unqualifiedgamer62523 жыл бұрын
Was messing around with silk . net a c# wrapper for stuff like opengl, vulkan, glfw and other common game development libraries. and had to get to try and understand unsafe, fixed and how pointers worked. it was very confusing at first, coming from .net mvc background. unfortunately I had to postpone the project indefinitely when covid happened, so I barely remember any of it anymore.
@jimread23542 жыл бұрын
That's an interesting video, I've been programming in C# for nearly 15 years now and I didn't know about the checked keyword. I actually use "unsafe" quite a lot when I'm calling Windows APIs because they deal with pointers quite often but if you're doing that, you kind of have to use "fixed" or else, like you mentioned your next look at the pointer might not be valid. I also use sizeof when I'm writing out custom binary data files when I don't have access to a database.
@gipsnichmeer3 жыл бұрын
When you started typing i thought: Stop! I never had ro use uint before lol
@HighwaysTravel3 жыл бұрын
For me it's checked and unchecked are new keywords. But others are good to revise 😁
@W1ngSMC3 жыл бұрын
Why doesn't the compiler just make every "new" you use in a local scope into stackalloc?
@sebasortix2 жыл бұрын
I was grindering some weed when you declared the snoopDog variable.... I felt so alluded... 🤣
@neilsg20013 жыл бұрын
Stackalloc performance gain?
@daniell.raharitahiana57273 жыл бұрын
I remember using unsafe code to write a weird implementation of a voronoi noise bitmap generator using pointers. It was more of an exercise than it was of any actual use, and it was slow as all heck, but it was definitely fun to mess around with lower level stuff in C#. But hey it's called unsafe for a reason, right? (•~•)
@MrMatthewLayton3 жыл бұрын
Why do you prefix member variables with an underscore?
@nickchapsas3 жыл бұрын
To distinct between local variables and class level variables
@MrMatthewLayton3 жыл бұрын
@@nickchapsas Sorry, as you may have seen already, I've added a comment to one of your other videos as to why I specifically don't add underscores for this reason.
@nickchapsas3 жыл бұрын
@@MrMatthewLayton That's cool, people don't have to use the same convensions
@KvapuJanjalia2 жыл бұрын
I was expecting to see likes of "__arglist", "__makeref", "__reftype" and "__refvalue" in these series, but all I saw were pretty normal everyday (or maybe, "every-month") keywords.
@DaddyFrosty2 жыл бұрын
I love the video but I feel like you were worried about people not understanding char* and -> when you haven't explained memory in the video. Those that understand memory don't need to worry about char*
@jerryplayz1013 жыл бұрын
On the subject of “stackalloc” is that at all similar to “malloc” from C? I haven’t looked too much into it, but if it means “memory allocate” (I.e. to RAM) and “stack allocate” for the prior- what does the other variation, “heapalloc” do?
@phizc3 жыл бұрын
malloc in C allocates on the heap. Stackalloc is like c arrays. E.g signed int numbers[3] = {4,2,0};
@jordangaines4963 жыл бұрын
`malloc` in C allocates memory on the heap, not on the stack.
@Keldor3143 жыл бұрын
"stackalloc" is like "malloca" from C I believe. And you have to be extremely careful with it since it will become deallocated the moment you return from the enclosing function and drop the stack frame. If you want data allocated on the stack, use a struct. That's what those are for.
@Keldor3143 жыл бұрын
Regarding "heapalloc", I believe that it just does what "malloc" would do in most cases. (And I believe it's native WIN32, so unmanaged) Evidentally you can also create multiple heaps, which I suspect is something more interesting for people writing the actual OS and maybe device drivers than ordinary programmers. I can imagine where from the OS's perspective, each process has its own private heap, but of course the OS has to make all that work.
@Cralis13 жыл бұрын
I'm trying to work out why they defailed to unchecked. Your example of the max value is good. The answer becomes wrong, I believe. Strange that you can't override to unchecked rather.
@ezra38713 жыл бұрын
No mention of "dynamic" in this and the sequel it's just that variable type is determined at runtime
@nickchapsas3 жыл бұрын
Dynamic is very common still, mainly due to packages like Dapper
@Terrados13373 жыл бұрын
I always just assumed c# had pointers :D Now that I know, I will have some fun with it :D Must leave unmanagable legacy code for generations to come
@igorthelight3 жыл бұрын
You! Get back here! :-)
@phizc3 жыл бұрын
It's hardly legacy code. Pointers are useful for interop and low level performant code. In dotnet5/C# 9 we've also now got function pointers. Stackalloc also returns a raw pointer by default, that's why he wrote "Span" instead of just "var".
@Terrados13373 жыл бұрын
@@igorthelight muhahah and now I build an undocumented nuget package and use botnets to download it constantly so its always on top of search results xD
@igorthelight3 жыл бұрын
@@Terrados1337 Ha-ha :-)
@nimrodel39243 жыл бұрын
I litterally use all of these daily, currently working on 3D graphics software 😉
@brentsteyn66713 жыл бұрын
I know a little bit why you would use it. But then... WHY?
@GazziFX5 ай бұрын
already used for years, except checked
@sewer56lol3 жыл бұрын
Aaaaa. I mod native games using C# on a regular basis and I practically use those keywords daily. I was expecting something of the likes of volatile ahahaha.
@superpcstation3 жыл бұрын
Without watching the video, here are three my guesses: Global Goto Unchecked
@superpcstation3 жыл бұрын
Lol i only got one right 😅
@nickchapsas3 жыл бұрын
You technically got Global right as well. It will be in part 2 :D
@lithium8203 жыл бұрын
i always imagined sizeof is pretty universal across langs
@DxCKnew2 жыл бұрын
DisplaySizeOf() method doesn't actually need to be marked as unsafe. Additionaly to the fixed keyword, there is GCHandle.Alloc(obj, GCHandleType.Pinned) in case you need to retain the pointer across multiple scopes and release it in completely another scope (like a Dispose() method or a finalizer). You can cheat and essentially bypass the need for the 'unsafe' keyword by using IntPtr and Marshal class for most of the pointer functionality (which is still unsafe).
@Pedro5antos_3 жыл бұрын
great vid, mate!
@fredhair3 жыл бұрын
Seems like many of these are low level enabling features which imo is better left to C or C++. C# is a great language but its never likely to get the same performance in these situations. If you want direct control of your memory you really should be using C++ or maybe rust? C# is meant to be high level and take care of low level operations for you at the expense of some performance impact. For most use cases C# is great and versatile and it can get closer to the hardware reasonably well, far better than many other high level languages e.g. python, java. StackAlloc seems to be the only one of these I would probably use in C#, for low level I would have to consider the overhead of calling into a C++ DLL vs just using these low level features.
@zhh1743 жыл бұрын
unsafe, sizeof, stackalloc are actually very useful for high-performance apps.
@TheIcecoldorange8 ай бұрын
Why are you using uint? Do you actually c#?
@godsonjoseph3 жыл бұрын
Does anyone know what IDE he uses?
@nickchapsas3 жыл бұрын
JetBrains Rider
@Odinhaus3 жыл бұрын
I have used them all, and then some :)
@renich66663 жыл бұрын
I've used all of them.
@ovidiurudi3 жыл бұрын
This part is usefull when you want to use some native dll functions you'll have to wrap up all those functions and "unsafe" & co will be your friends :)
@Ownage4lif313 жыл бұрын
Or couldn't you just use dll import? I've never had an issue using dll import to use my c++ functions. Barely ever used it but it works. Not sure on the limitations though
@ovidiurudi3 жыл бұрын
@@Ownage4lif31 right now it can be. Ten years ago when I made some wrappers over few Delphi libraries I had to do that. One of the libraries was a protocol over TCP/IP and I had some issues about how data types are stored. Honestly speaking I don't remember exactly what was the issue but when an c# app send the data to our delphi ecosystem apps it was received corrupted. Happy codding!
@ashisheady88413 жыл бұрын
you never need class keyword you can trust me.
@ppaliwal893 жыл бұрын
Okay, I might sound dumb but what IDE is that?
@nickchapsas3 жыл бұрын
It's JetBrains Rider
@ppaliwal893 жыл бұрын
@@nickchapsas before I go compare it with Visual Studio, what are you top reasons to use it?
@ppaliwal893 жыл бұрын
@@nickchapsas don't bother, looks like you already got it covered, am gonna check your other video out :)
@HamishArb3 жыл бұрын
I use all of those very often, except stackalloc (since it can stack overflow) because it runs faster. I was expecting the 4 __ keywords to be on this video lol. I do not use the __ keywords often lol.
@darioabbece39483 жыл бұрын
I guess fixed could be used if you were to manage password or encription
@corruptconverter26163 жыл бұрын
Some other keywords: __arglist, __makeref, __refvalue, __reftype
@promant64583 жыл бұрын
You could also include the 'sbyte' keyword. Like, literally, why would anybody want to use it?
@JohnLudlow3 жыл бұрын
@@alexanderboll235 The docs recommend Int16 instead. Which only uses twice as much memory...
@JohnLudlow3 жыл бұрын
It's a CLS compliance thing. docs.microsoft.com/en-us/dotnet/api/system.sbyte?view=net-5.0 Not sure why it's not CLS compliant but there you go
@anonimzwx Жыл бұрын
unsafe is used sometimes when you do renders
@ankushmadankar17563 жыл бұрын
Informative!!
@shreyasjejurkar12333 жыл бұрын
I think restriction of sizeof is not unmanaged, it's should be value type and not reference type!
@nickchapsas3 жыл бұрын
It is value type
@aybarsacar93753 жыл бұрын
I didn’t know c# had pointers capability. And the unsafe syntax is the same as c++ syntax
@st0ox3 жыл бұрын
From now on I will always call my unsafe pointers snoopDog
@AlexxXRecorD3 жыл бұрын
Nice, thanks so much!)))
@shayvt3 жыл бұрын
Why stackalloc is more safe? I didn't catch that.
@nickchapsas3 жыл бұрын
It’s not more safe. It is also safe because it had buffer overflow protection baked into it.
@Keldor3143 жыл бұрын
More safe than its C counterpart! Which is a really good thing, since overflowing a buffer on the stack is just about the oldest security exploit in the book. The thing about the stack that makes it such a tempting target is that it has the same layout (within a given function, anyway) every time the program is run, and that it also contains a return address to tell the program where to go back to after it returns from the given function. Once an attacker is able to mess with *that*, it's pretty much game over.
@AB-cn5hb3 жыл бұрын
What editor is he using? Debugging looks nice.
@nickchapsas3 жыл бұрын
I'm using JetBrains Rider
@Gastell03 жыл бұрын
You definitely used sizeof if you ever dealt with any Windows API with Interops and Marshaling via P/Invoke, pinvoke website is life
@CRBarchager3 жыл бұрын
I knew about all these but don't use them in my day to day coding. They history behind these keyword goes back to the very early versions of C# when C# didn't have a ton of libraries with managed code and you had to interact alot more with c++ libraries in Windows than you do now.
@brentsteyn66713 жыл бұрын
I did not know that, very interesting🤔
@DanGolick2 жыл бұрын
I don’t think you can count on a .net string to be terminated with ‘\0’