我创建了一个 using() 而不指定对象名称。
我的问题是如何访问我的新对象并打印它的名称?
class Program
{
static void Main(string[] args)
{
AnimalFactory factory = new AnimalFactory();
using (factory.CreateAnimal())
{
Console.WriteLine("Animal {} created inside a using statement !");
//How can i print the name of my animal ?? something like this.Name ?
}
Console.WriteLine("Is the animal still alive ?");
}
}
public class AnimalFactory
{
public IAnimal CreateAnimal()
{
return new Animal();
}
}
public class Animal : IAnimal
{
public string Name { get; set; }
public Animal()
{
Name = "George";
}
public void Dispose()
{
Console.WriteLine("Dispose invoked on Animal {0} !", Name);
Name = null;
}
}
public interface IAnimal : IDisposable
{
string Name { get; }
}