DonBoitnott
DonBoitnott

Reputation: 11025

What is the equivalent syntax of this C++ code snippet in C#?

Because I do not know what this type of construct is even called, I do not know how to begin searching for an answer, so I am asking for it directly: what is the C# syntax equivalent of this code from C++?

#define DoExit { \
    if (pDialog) delete pDialog; \
    if (lib) FreeLibrary(lib); \
    if (MadeNew) delete pRS; \
    return retval;}

This was taken from within a class method, so it's an inline declaration of a function to be called later. In fact, here's an example of where it was used:

if (pRS->GetSize() == 0) DoExit

Note the lack of either () or a terminating semi-colon.

My first guess was some kind of inline delegate construct, but I am also not well versed in those, so it's but a guess.

So have at it, all you C++ gurus out there!

Upvotes: 2

Views: 198

Answers (3)

Felice Pollano
Felice Pollano

Reputation: 33262

The equivalent of this code in C# is

;

because:

delete something

is done by the garbage collector, so non need to do it. Libraries ( ie Assembly are managed by .NET framework, so strictly speaking you don't manage loading unloading of assemblies, unless you are loading unmanaged libraries ) The single portion you can find an equivalent is the FreeLibrary if you did a LoadLibrary P/Invoke somewhere else. In this case have a look here: FreeLibrary.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564611

This is a macro in C++ - you basically just add that code, so the "actual" code would be:

if (pRS->GetSize() == 0)
{ 
    if (pDialog) delete pDialog;
    if (lib) FreeLibrary(lib); 
    if (MadeNew) delete pRS; 
    return retval;
}

There is no way to do the equivalent directly in C#. You would need to expand the code manually, then port the expanded code as needed, or convert the macro to a method and call it.

Upvotes: 1

Adam Maras
Adam Maras

Reputation: 26863

What you've encountered is called a macro. There is no equivalent in C#.

Upvotes: 0

Related Questions