1

In VB OLE the object is set to Nothing.
I am porting now this code to C#.
Must I set this object in C# to null or is this not necessary?

VB

Set Session oSession = CreateObject("session")
...
Set oSession = Nothing

In C#

var oSession = new Session();
...
oSession = null;

Is this the same?

GSerg
  • 76,472
  • 17
  • 159
  • 346
ora
  • 115
  • 1
  • 1
  • 12
  • 1
    @RobertHarvey - https://stackoverflow.com/a/38111137/2598770 | https://stackoverflow.com/a/38111294/2598770 | https://stackoverflow.com/a/25135685/2598770 – Rand Random May 31 '23 at 13:06
  • 2
    In general, everything that implements `IDisposable` should be disposed. Other then that, i don't know your code, neither do i know where that`Session` class origins from. – nilsK May 31 '23 at 13:18
  • The following may be of interest: [Early and Late Binding (Visual Basic)](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late-binding/). – Tu deschizi eu inchid May 31 '23 at 15:06
  • 2
    That's not VB6 code. It would look like `Set oSession = CreateObject("session")` and `Set oSession = Nothing` – Étienne Laneville May 31 '23 at 15:37

2 Answers2

2

Is this the same?

No.

In VB6 code the session is destroyed as soon as you set the variable to Nothing (provided that you haven't saved it into another variable too).

In C# the session keeps living and will be released at some point later (or not).

If you want to destroy the session at that point, and it doesn't implement IDisposable, then force GC to collect it. Setting the variable to null is then not required, provided that you are not referencing that variable later in the code.

GSerg
  • 76,472
  • 17
  • 159
  • 346
0

The Session class is type library class which does not implement IDisposable. So I conclude Marshall.ReleaseCOMObject is not necessary.

ora
  • 115
  • 1
  • 1
  • 12