Kevin Scheidt
Kevin Scheidt

Reputation: 95

Interface / IDisposable with Object Initializers and Properties

as a follow up to the question answered at How do I structure a class or a function/method (or interface) so that as part of a using, I can pass values in the {} brackets?,

how do I utilized object Initializers and Properties with an Interface Structure:

public class SomeClassHelper : ISomeClassHelper 
{
}

public class SomeClass: IDisposable
{
    public SomeClass(object somevalue) {
    }

    public int AnotherValue { get; set; }

    public int AdditionalValue { get; set; }

    internal void ImHere() {
        throw new NotImplementedException();
    }

    public void Dispose() {
        throw new NotImplementedException();
    }
}

static void Main(string[] args) {
    object somevalue = null;
    using (var something = new SomeClass(somevalue) { AnotherValue = 1, AdditionalValue = 2 }) {
        something.ImHere();
    }
}

how would I use the above so that I could have the interface, or does the purpose of an interface break the above?

Upvotes: 1

Views: 110

Answers (1)

James D
James D

Reputation: 390

I think you may be misunderstanding the purpose of an interface.

From https://msdn.microsoft.com/en-us/library/ms173156.aspx:

An interface contains definitions for a group of related functionalities that a class or a struct can implement. By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes. In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class.

An interface is to force any class that implements it, to meet certain conditions, so that other code can then rely on that class having certain commonalities with any other class implementing the same interface.

So in your other question, people have responded advising that if you wish to use a using block, your class would need to implement the IDisposable interface. This is because, for using to work, it needs to know that the class it is working with, is going to have a method, called Dispose(), that frees up any unmanaged resources in use by the class.

If your SomeClass doesn't have any unmanaged resources, it might be wise to revisit what it is you are trying to do. The garbage collector will automatically dispose of managed objects (like instances of your class) when they stop being used (I'm generalizing a bit here).

This should really have been answered on your other question, but what you might want to explore doing, (instead of using the initializer), is to create a constructor for your class, and use that when instantiating your class.

You'd end up with something like the following:

    public class SomeClass: IDisposable
    {
        public SomeClass(object somevalue, int anotherValue, int additionalValue)
        {
            AnotherValue = anotherValue;
            AdditionalValue = additionalValue;
        }

        public int AnotherValue { get; set; }

        public int AdditionalValue { get; set; }

        internal void ImHere()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }

    static void Main(string[] args)
    {
        object somevalue = null;
        using (var something = new SomeClass(somevalue, 1, 2))
        {
            something.ImHere();
        }
    }

You can read more on using constructors at https://msdn.microsoft.com/en-us/library/ms173115.aspx.

Upvotes: 1

Related Questions