为什么我不能公开一个实现的接口方法?

时间:2012-04-15 15:29:29

标签: c# interface

我一直在尝试一些n层架构,我真的很想知道为什么这段代码不会编译......

它表示修饰符public对此项无效。但为什么不呢?我需要能够从BLL对象访问项目IRepository.AddString(),但它不会让我公开....

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();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

这是explicitly-implemented member,它始终是私密的。

从声明中删除IStringRepo.以创建也实现接口的普通公共成员。

答案 1 :(得分:0)

显式实现的接口不能使用可见性修饰符。

public List<string> IStringRepo.GetStrings() 

应该是:

public List<string> GetStrings()