Skip to content

Add new ObjectDisposedException constructor overload #51700

Closed
@marek-safar

Description

@marek-safar

Background and Motivation

There is a very common pattern used to handle disposed object state and it involves throwing ObjectDisposedException. It requires extra conversion before constructing the exception. There are about 400 cases of this pattern in runtime which could be unified and simplified and more in .NET libraries.

	throw new ObjectDisposedException(GetType().FullName);
	// or
	throw new ObjectDisposedException(GetType().Name);
	// or
	throw new ObjectDisposedException(GetType().ToString());
	// or
	throw new ObjectDisposedException(nameof(NameOfThisType));

Proposed API

namespace System
{
     public class ObjectDisposedException {
+		public ObjectDisposedException(object instance);
     }

Usage Examples

All the examples from above could be replaced with simple construction which would extract the name from the instance.

	throw new ObjectDisposedException(this);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions