小编Mic*_*iva的帖子

为什么我必须使用IoC统一

我创建了一个适用于依赖性反转原理的类,并使用了依赖注入模式,如下所示:

public interface ITypeOfPrinter
{
    string Printing();
}

public class Print
{  
    private readonly ITypeOfPrinter _typeOfPrinter;

    public Print(ITypeOfPrinter typeOfPrinter)
    {
        _typeOfPrinter = typeOfPrinter;
    }

    public string print()
    {
        return _typeOfPrinter.Printing();
    }
}

public class BlackAndWhitePrinter : ITypeOfPrinter
{
    public string Printing()
    {
        NumberOfPrints++;
        return string.Format("it is Black and white printing {0}", NumberOfPrints);
    }

    public int NumberOfPrints { get; set; }

}
public class ColorfullPrinter : ITypeOfPrinter
{
    public string Printing()
    {
        NumberOfPrints++;
        return string.Format("it is colorfull printing {0}", NumberOfPrints);
    } …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing design-patterns inversion-of-control unity-container

4
推荐指数
2
解决办法
355
查看次数

删除除给定索引号之外的所有元素

我有以下列表,如何从linq中删除给定索引号的所有元素:

    List<string> a = new List<string>();
        a.Add("number1");
        a.Add("number2");
        a.Add("number3");
Run Code Online (Sandbox Code Playgroud)

如何使用linq删除除索引号= 2的元素之外的所有元素.

c# linq

3
推荐指数
1
解决办法
6727
查看次数

我有哪种设计模式

我有两个类,每个类都做同样的事情但只有差异,它们在代码中的某些函数中使用不同的逻辑.让我们说:

class A
{
    //has same fields, and method
    void GetDataTable()
    {
         //logic here changes up to table of the database and for some fields.
    }
}

class B
{
    //has same fields, and method
    void GetDataTable()
    {
         //logic here changes up to table of the database and for some fields.
    }
}
Run Code Online (Sandbox Code Playgroud)

在一天结束时,我会添加另一个GetDataTable具有不同逻辑的行为类和方法.我需要采用什么样的设计模式或OO技术才能获得更高质量的代码.

c# design-patterns

3
推荐指数
1
解决办法
231
查看次数

接口类型可以包含哪些类型?

我有一些班级类型:

class MyClass
{
    …
}
Run Code Online (Sandbox Code Playgroud)

我想知道在接口中包含这种类型的对象是好还是坏?

interface IInterface
{
   List<MyClass> _myobj { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是不好的做法吗?这是否意味着IInterface取决于MyClass因为我从这种类型引用它?

c#

2
推荐指数
1
解决办法
73
查看次数

将列表分配给另一个列表

我有以下代码,我不明白为什么我的主要方法列表能够改变或影响另一个列表?

 class Program
    {
        static void Main(string[] args)
        {
            List<string> t = new List<string>();
            t.Add("a");
            t.Add("b");
            t.Add("c");


            B b = new B(t);
            b.Add();
            Console.WriteLine(t.Count.ToString()); //why Output 4


        }
    }

    class B
    {
        public List<string> mylist2 { get; set; }
        public B(List<string> lsarg)
        {
            mylist2 = new List<string>(); //new allocate new location?
            mylist2 = lsarg;
        }
        public void Add()
        {
            mylist2.Add("hi");
        }
    }
Run Code Online (Sandbox Code Playgroud)

在B类的构造函数中,我已经将新位置分配给新字段为mylist2.

c#

-1
推荐指数
1
解决办法
72
查看次数