【发布时间】:2013-12-11 09:11:14
【问题描述】:
我想使用单例模式来创建一个缓冲区来存储所有需要的图片。
类似的东西:
public sealed class BaseBuffer
{
private static readonly Dictionary<string, Bitmap> pictures = new Dictionary<string, Bitmap>();
public static Bitmap GetPicture(string name, ref Bitmap output)
{
//In case the pictureBuffer does not contain the element already
if (!pictures.ContainsKey(name))
{
//Try load picture from resources
try
{
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("MyProject.Resources." + name + ".png"));
pictures.Add(name, bmp);
return bmp;
}
catch (Exception ex)
{
Console.WriteLine("Picture {0} cannot be found.", name);
}
}
else
{
return pictures[name];
}
return null;
}
现在我不确定。 “GetPicture”方法是返回图片的副本还是返回引用?
应用程序需要一些图片,这些图片经常在不同的应用程序/表单上显示。因此,最好只引用图片而不是复制它们。
你知道怎么做吗?
【问题讨论】:
标签: c# memory reference singleton