添加到List<>?时避免实例化类X次

时间:2015-09-23 21:48:14

标签: c# oop

我有class StatsFile,如下所示。它有两个字段,用于保存两个字符串,这些字符串将添加到List<>

public class StatsFile
{
    public string RemoteFileName;
    public string LocalFileName;
}

然后我有另一个class FileProcessormethod GetAllFiles填充List<>多个StatsFile s:

class FileProcessor
{
    public List<StatsFile> GetAllFiles()
    {
        List<StatsFile> fileList = new List<StatsFile>();

        StatsFile weekFiles = new StatsFile();

        weekFiles.RemoteFileName = "Remote\file1";
        weekFiles.LocalFileName = "Local\file1";
        fileList.Add(weekFiles);

        weekFiles.RemoteFileName = "Remote\file2";
        weekFiles.LocalFileName = "Local\file2";
        fileList.Add(weekFiles);

        weekFiles.RemoteFileName = "Remote\file3";
        weekFiles.LocalFileName = "Local\file3";
        fileList.Add(weekFiles);

        return fileList;
    }           
}

只是StatsFile的一个实例,List<>中的所有项目都具有相同的值。我可以实例化StatsFile 3次来解决问题,但还有另一种方法可以重用同一个实例吗?或者更好的方式?

3 个答案:

答案 0 :(得分:1)

您可以在构造期间初始化变量:

class FileProcessor
{
    public List<StatsFile> GetAllFiles()
    {
        List<StatsFile> fileList = new List<StatsFile>();

        fileList.Add(new StatsFile {
            RemoteFileName = "Remote\file1",
            LocalFileName = "Local\file1"
        });

        fileList.Add(new StatsFile {
            RemoteFileName = "Remote\file2",
            LocalFileName = "Local\file2"
        });

        fileList.Add(new StatsFile {
            RemoteFileName = "Remote\file3",
            LocalFileName = "Local\file3"
        });

        return fileList;
    }           
}

答案 1 :(得分:1)

使用 struct 代替StatsFile类型的类。

public struct StatsFile
{
    public string RemoteFileName { get; set; }
    public string LocalFileName { get; set; }
}

class FileProcessor
{
    public List<StatsFile> GetAllFiles()
    {
        var fileList = new List<StatsFile>();
        StatsFile weekFiles;

        weekFiles.RemoteFileName = @"Remote\file1";
        weekFiles.LocalFileName = @"Local\file1";
        fileList.Add(weekFiles);

        weekFiles.RemoteFileName = @"Remote\file2";
        weekFiles.LocalFileName = @"Local\file2";
        fileList.Add(weekFiles);

        weekFiles.RemoteFileName = @"Remote\file3";
        weekFiles.LocalFileName = @"Local\file3";
        fileList.Add(weekFiles);

        return fileList;
    }           
}

以下是结果

FileProcessor fp = new FileProcessor();
var result = fp.GetAllFiles();
result.Dump();

enter image description here

答案 2 :(得分:0)

除了此处给出的所有答案外,您还可以向StatsFile课程添加方法,并将其命名为AddCurrentInstance。此方法可以接收List<StatsFile>作为输入并将自我克隆添加到其中。

因此,StatsFile将采用以下方法:

public void AddCurrentInstance (List<StatsFile> listings)
{
    if (listings == null)
    {
        Console.WriteLine("Received null listings. Returning...");
        return;
    }
    listings.Add(this.clone());
}

现在,您的电话会变成以下内容:

public List<StatsFile> GetAllFiles()
{
    List<StatsFile> fileList = new List<StatsFile>();
    StatsFile weekFiles = new StatsFile();

    weekFiles.RemoteFileName = "Remote\file1";
    weekFiles.LocalFileName = "Local\file1";
    weekFiles.AddCurrentInstance(fileList);

    weekFiles.RemoteFileName = "Remote\file2";
    weekFiles.LocalFileName = "Local\file2";
    weekFiles.AddCurrentInstance(fileList);

    weekFiles.RemoteFileName = "Remote\file3";
    weekFiles.LocalFileName = "Local\file3";
    weekFiles.AddCurrentInstance(fileList);

    return fileList;
}