1

Say I have a 3D double array 3dArray, and a variable of custom type Person person1. Doing this:

3dArray = null;
person1 = null;

works but looks sloppy and doing this:

3dArray = person1 = null; 

as throws an error as 3dArray and person1 can't be implicitly converted. Is there a better way to do this in one line?

5
  • 3
    Why do this at all? The GC is more than capable of handling this. Use IDisposable() when applicable and just let the rest happen. By setting the variable to null you could actually be extending the life of it and preventing the GC from doing its job Commented Aug 14, 2018 at 18:28
  • @maccettura That makes sense. Thank you. However, my 3D array takes about 2GB of memory and does not implement IDisposable. I've noticed my application can get to around 4-5GB of memory usage when I know that is unnecessary. Should I set it to null? Commented Aug 14, 2018 at 18:39
  • this may be possible with later versions of c# 8, as the going to change how primitive will be able to be null-able, but probably not and also would argue... this would violate the whole point of a typed language!, if they were of the same type then sure. Commented Aug 14, 2018 at 18:58
  • 2
    further your title is very specific... making reference to "for memory management" none of the above is really what would affect memory management, in terms of "memory management" the above is just semantics. if you doing this because you think your affecting memory management, your wrong! Commented Aug 14, 2018 at 19:05
  • IDisposable is not responsible for releasing memory, that is the job of the garbage collector. Setting a variable to null is not necessary as the gc will know if the object is no longer reachable and will collect it as needed. When this happens is not deterministic. It should happen when there is memory pressure. Commented Aug 14, 2018 at 19:53

3 Answers 3

3

No, there is not. C# has no special provision by which null propagates regardless of variable type. It is otherwise strong typed, so this assignment IS not legal (as the first assigment determines the variable type).

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, it can be done with implicit operator overloading for every class. It's awful solution because I believe the given types are not logically convertable from one to another. But it will work regardless of it's null or not.
0

If you simply want it on one line,

3dArray = null; person1 = null; something_else = null;

Doesn't save anything from a typing perspective, but it is more vertically dense code.

2 Comments

While it fits the requirements, its a bad idea. Each line should have its own semi-colon
It is strictly a matter of taste. I personally don't mind when vertical space is preserved for technically insignificant minutiae.
0

You can do it but only if the types have some sort of relationship and you assign from specific to general. For example, this works:

    string s;
    object o;
    IEnumerable l;

    o = l = s = null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.