Why cant I expose an implemented interface method?

♀尐吖头ヾ 提交于 2019-12-24 04:14:17

问题


I've been trying out some n-tier architecture and im really wondering why this code wont compile...

It says the modifier public is not valid for this item. But why not? I need to be able to access the item IRepository.AddString() from a BLL object but it just wont let me make it public....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BLL myBLL = new BLL();

        }
    }

    interface IRepository<T>
    {
        void AddString();
    }

    interface IStringRepo : IRepository<string>
    {
        List<string> GetStrings();
    }

    public class BLL : IStringRepo
    {
        public List<string> FilterStrings()
        {
            return new List<string>() { "Hello", "World" };
        }

        public List<string> IStringRepo.GetStrings()
        {
            throw new NotImplementedException();
        }

        public void IRepository<string>.AddString()
        {
            throw new NotImplementedException();
        }
    }
}

回答1:


That's an explicitly-implemented member, which is always private.

Remove IStringRepo. from the declaration to create a normal public member that also implements the interface.




回答2:


Explicitly implemented interfaces cannot use visibility modifiers.

public List<string> IStringRepo.GetStrings() 

should be:

public List<string> GetStrings() 


来源:https://stackoverflow.com/questions/10163384/why-cant-i-expose-an-implemented-interface-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!