cap5lut
cap5lut
CC#
Created by Sieg on 2/6/2025 in #help
Help me with C++ / C#
@Siegthis sounds a lot like u want to write some unit or integration tests. the question is what u really want to test. use the .NET test frameworks to test the C# side. use whatever c++ provides to test their side. testing the interoperability will become quite a big task
17 replies
CC#
Created by Sieg on 2/6/2025 in #help
Help me with C++ / C#
@Anton as far as i know .NET doesnt provide functionaility for c++ interop but only for C, and generally speaking they want to "reverse p/invoke"
17 replies
CC#
Created by Sieg on 2/6/2025 in #help
Help me with C++ / C#
basically for what you are looking for u are most likely need to host the runtime inside ur native code, unless u want to restrict urself to NAOT compiled libraries/programs, which itself is explained there: https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting and thats just a starter; you will need to read the whole (or at least most of the) "Native interoperablity" section
17 replies
CC#
Created by Sieg on 2/6/2025 in #help
Help me with C++ / C#
secondary u have to take into account, that C# (or .NET) uses garbage collection instead of explicitly freeing memory. and at that a compacting one, thus on the C# side, the addresses of references could change and make them invalid, if they were created on the C# side.
17 replies
CC#
Created by Sieg on 2/6/2025 in #help
Help me with C++ / C#
you can compile via NAOT to expose methods via the [UnmanagaedCalleryOnly] attribute. though, NAOT comes with with some restrictions.
17 replies
CC#
Created by Sieg on 2/6/2025 in #help
Help me with C++ / C#
if u want to call a c# method from c++ you have a lot to consider, first of that C# (or .NET) is usually not natively compiled in first place and thus do not expose functions/methods like ur typical C or C++ functions
17 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
(as a side note, there is actually a third kind of exception: the uncatchable, these u will not see unless u do something unsafe that is close to the system, its the kind of problems where the process itself can not recover from)
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
there are in the end 2 kinds of exceptions, one that indicate bugs (like ArgumentException, NullReferenceException) which u usually do not catch (or only catch them to log what went wrong) the other kind is failure that is outside of ur control (eg the internet connection died because ur wifi sucks, or the human ripped out the usb stick while u write to that file)
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
when to throw exceptions is quite use case dependent. ur math example: this is a bit more complex because the existence of NaN, its a value to indicate that an operation wasnt possible, without throwing. thats because often u have quite complex computations going on there and then check at certain points if its NaN to see if there is a failure (u dont want a try catch block for every division, do ya?) for the age of a person: as with each other, this type should maintain a stable state, because a negative age isnt possible, its the callers fault to provide the wrong values and it should throw an exception (an ArgumentOutOfRangeException in this case)
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
basically u want the state of ur class/type to be valid all the time, lets assume the DoSomethingThatMightThrow() method modifies ur internal state. due to the exception that state would become invalid the next time it is used, but catch the exception, fix the internal state to make it valid again, but still provide the caller with the exception of what went wrong
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
there is also one case i didnt mention yet, rethrow, basically u have something like
try
{
DoSomethingThatMightThrow();
}
catch (XyzException)
{
// #
throw;
}
try
{
DoSomethingThatMightThrow();
}
catch (XyzException)
{
// #
throw;
}
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
throwing an exception is basically "hey something unexpected happened, here is the error, now clean up that mess yourself"
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
so its the callers responsibility to handle that error. that might be a closing the application/exiting the process or maybe showing an error dialog, or something completely different
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
eg, File.ReadAllBytes(string path) doesnt know how to handle the unexpected case that the file does not exist. to signal that to the caller, it throws an exception.
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
u basically throw an exception when you dont know what to do with that unexpected error
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
think about it like that. u want to try to open a door, instead of opening the door but if its locked u activate some alarm
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
a good example is $tryparse
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
one little side addition: exceptions should be exceptions, dont use them for normal control flow, they are pretty expensive under the hood performance wise
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
oh yeah that as well!
85 replies
CC#
Created by Faker on 2/1/2025 in #help
Exception Handling in C#
btw, i really love how u ask questions 🙂 u explain your thoughts and assumptions, not like others that paste code and go like "whats wrong?". so, dont start ur questions with being sorry, we are here to help 😉
85 replies