A
Alx Sharp
Hello...
When you call the Stack.Peek() method, it returns a reference to the object
at the top of the stack, but the object is not removed from the collection.
In the other hand, the method Stack.Pop() returns a reference to the object
at the top, but it does remove the object from the collection.
Notice that the object is removed from the stack but it isn't destroyed.
I'll show you an example to make it easier to understand.
Let's say I've created a class named Employee with the properties EmployeeID
of type int, FirstName of type string and LastName of type string.
If I use a member variable of type Stack (say it's named myStack) and some
variables of type Employee I can write code like:
myStack.Push(emp1); // Inserts an instance of Employee (emp1) at the top of
the stack
Employee emp2 = (Employee) myStack.Peek(); // Returns a reference (in emp2)
of the object at the top but doesn't remove it from the stack
Employee emp3 = (Employee) myStack.Pop(); // Returns a reference (in emp3)
of the object at the top but it's removed from the stack
When I call Stack.Peek() or Stack.Pop() I can do anything I want with the
reference returned by the method (emp2 or emp3), the diference is in that
with Stack.Peek(), the object remains in the stack and with Stack.Pop the
object is removed from the collection (not destroyed).
If you want to make a copy of your object, your class (in my example
Employee) must implement the interface ICloneable wich only has a single
method: ICloneable.Clone().
The method ICloneable.Clone() returns a new instance of your class with the
same value of the original object.
For my class Employee to implement ICloneable I have to write:
public class Employee : ICloneable
{
// ..
// Any member declaration goes here
// ..
// You can write "public object ICloneable.Clone()" instead of just
"public object Clone()" to make it clearer to read when implementing
multiple interfaces
// public object ICloneable.Clone()
public object Clone()
{
Employee newEmployee = new Employee();
newEmployee.EmployeeID = this.EmployeeID;
newEmployee.FirstName = this.FirstName;
newEmployee.LastName = this.LastName;
return newEmployee;
}
}
I hope I will be helpful for you...
Bye...
When you call the Stack.Peek() method, it returns a reference to the object
at the top of the stack, but the object is not removed from the collection.
In the other hand, the method Stack.Pop() returns a reference to the object
at the top, but it does remove the object from the collection.
Notice that the object is removed from the stack but it isn't destroyed.
I'll show you an example to make it easier to understand.
Let's say I've created a class named Employee with the properties EmployeeID
of type int, FirstName of type string and LastName of type string.
If I use a member variable of type Stack (say it's named myStack) and some
variables of type Employee I can write code like:
myStack.Push(emp1); // Inserts an instance of Employee (emp1) at the top of
the stack
Employee emp2 = (Employee) myStack.Peek(); // Returns a reference (in emp2)
of the object at the top but doesn't remove it from the stack
Employee emp3 = (Employee) myStack.Pop(); // Returns a reference (in emp3)
of the object at the top but it's removed from the stack
When I call Stack.Peek() or Stack.Pop() I can do anything I want with the
reference returned by the method (emp2 or emp3), the diference is in that
with Stack.Peek(), the object remains in the stack and with Stack.Pop the
object is removed from the collection (not destroyed).
If you want to make a copy of your object, your class (in my example
Employee) must implement the interface ICloneable wich only has a single
method: ICloneable.Clone().
The method ICloneable.Clone() returns a new instance of your class with the
same value of the original object.
For my class Employee to implement ICloneable I have to write:
public class Employee : ICloneable
{
// ..
// Any member declaration goes here
// ..
// You can write "public object ICloneable.Clone()" instead of just
"public object Clone()" to make it clearer to read when implementing
multiple interfaces
// public object ICloneable.Clone()
public object Clone()
{
Employee newEmployee = new Employee();
newEmployee.EmployeeID = this.EmployeeID;
newEmployee.FirstName = this.FirstName;
newEmployee.LastName = this.LastName;
return newEmployee;
}
}
I hope I will be helpful for you...
Bye...