-3

I have created a using() without specifying object name.
My question is how can i access my new object and print its name ?

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; }
}
ilansch
  • 4,784
  • 7
  • 47
  • 96

4 Answers4

6

Why do you want to do that? If you want access to the object here you should get a reference to it. (Assuming your example is representative of the problem you're trying to solve).

using (Animal a = factory.CreateAnimal())
{
   Console.WriteLine("Animal {0} created inside a using statement !", a.Name); 
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I would like to avoid it, to make my code as simple as it can be without declare the object.. – ilansch Aug 04 '13 at 13:40
  • @ilansch: but you already have created an object. What do you try to avoid? – Dennis Aug 04 '13 at 13:41
  • I am looking for a way to do this, but without create var = .. and then without writing var.Name inside the output. im not saying its possible, just trying to make a way to do it shortest. – ilansch Aug 04 '13 at 13:44
  • 3
    What is the point of "shortest", @ilansch? It won't make your code any faster, and it certainly doesn't make it any clearer to manipulate an object to which there are otherwise no references in the code. – Cody Gray - on strike Aug 04 '13 at 14:01
  • Declaring a variable introduces complexity? – Mathieu Guindon Aug 04 '13 at 14:32
4

You can't. Declare variable:

using (var animal = factory.CreateAnimal())
{
}
Dennis
  • 37,026
  • 10
  • 82
  • 150
2

The other answers are correct. However, I'd like to throw a bit of the language specification in here (instead of saying "you can't")

Page 259:

A using statement of the form

using (expression) statement

has the same three possible expansions. In this case ResourceType is implicitly the compile-time type of the expression, if it has one. Otherwise the interface IDisposable itself is used as the ResourceType. The resource variable is inaccessible in, and invisible to, the embedded statement.

So, what you want to do is explicitly forbidden by the spec.

Community
  • 1
  • 1
DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45
0

Maybe what you want to accomplish is like Object Pascal Delphi

with MyClass.Create() do
try
   // Access method an properties of MyClass in here
finally
    free;
end;

I haven't found that in any other language, in C# you need to declare the variable:

(using MyClass a = new MyClass())
{
    a.properities/methods;
}

The only extra code that you don't need to code is the finally free.

ja_mesa
  • 1,971
  • 1
  • 11
  • 7
  • Yes, Delphi allows this. And it is often considered bad practice because it allows for subtle scope changes when upgrading Delphi versions. (http://stackoverflow.com/questions/514482/is-delphi-with-keyword-a-bad-practice) – DasKrümelmonster Aug 04 '13 at 14:05