Languages

C# - Error CS1674 "ILogger": The type used in the using statement must be implicitly convertible to the interface "System.IDisposable"

0 points
Asked by:
Gigachad2137
1000

CommonLogger.cs file:

namespace ConsoleApp.Logger
{
    class CommonLogger : ILogger
    {
        private ILogger[] loggers;

        public CommonLogger(ILogger[] loggers)
        {
            this.loggers = loggers;
        }

        public virtual void Log(params String[] messages)
        {
            
        }

        public void Dispose(bool disposing)
        {
            this.Dispose(disposing: true);

            GC.SuppressFinalize(this);
        }
    }
}

 

Program.cs file: 

public class Program
{
    public static void Main()
    {
        ILogger[] loggers = new ILogger[]
        {
            new ConsoleLogger(),
            new FileLogger(),
            new SocketLogger(host:"google.com",port:80)
        };

        using (ILogger logger = new CommonLogger(loggers))
        {
            logger.Log("Example message 1 ...");
            logger.Log("Example message 2 ...");
            logger.Log("Example message 3 ...", "value 1", "value 2", "value 3");
        }
    }
}

ILogger.cs file: 

namespace ConsoleApp.Logger
{
    public interface ILogger : IDisposable
    {
      void Log(params String[] messages);
    }
}

IDisposable.cs file:

namespace ConsoleApp.Logger
{
    public interface IDisposable
    {
    }
}

 

The question was modified by moderator to make it more readable.

 

1 answer
1 points
Answered by:
Gigachad2137
1000

Looking on the question title I think you have problem with using statement and IDisposable interface.

C# by default provides IDisposable interface that can be used to release some resources (unmanaged resources). Classes that implement IDisposable intereface may be used with using statement that releases resouces automatically when the using statement execution is ended.

But your CommonLogger class implements your own IDisposable interface that you want to use later in using (ILogger logger = new CommonLogger(loggers)). The solution for the Error CS1674 is to remove your IDisposable interface and use the one provided by .NET.

 

References

  1. IDisposable Interface - Microsoft Docs

  2. using statement - Microsoft Docs

0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join