Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 602679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:50:54+00:00 2026-05-13T16:50:54+00:00

By calling Push() and Pop() an instance of Stack<T> in a single line I

  • 0

By calling Push() and Pop() an instance of Stack<T> in a single line I get a different behavior than performing the imho same code in two lines.

The following code snippet reproduces the behavior:

static void Main(string[] args)
{
 Stack<Element> stack = new Stack<Element>();
 Element e1 = new Element { Value = "one" };
 Element e2 = new Element { Value = "two" };
 stack.Push(e1);
 stack.Push(e2);

 Expected(stack);  // element on satck has value "two"
 //Unexpected(stack);  // element on stack has value "one"

 Console.WriteLine(stack.Peek().Value);
 Console.ReadLine();
}

public static void Unexpected(Stack<Element> stack)
{
 stack.Peek().Value = stack.Pop().Value;
}

public static void Expected(Stack<Element> stack)
{
 Element e = stack.Pop();
 stack.Peek().Value = e.Value;
}

The Element class is really basic:

public class Element
{
 public string Value
 {
  get;
  set;
 }
}

With this code I get the following result (.NET 3.5, Win 7, fully patched):

  • Calling Expected() (version with
    two lines) leaves one element on the
    stack with Value set to "two".
  • When calling Unexpected() (Version
    with one line) I get one element on
    the stack with the Value set to
    "one".

The only reason for the difference I could imagine was the operator precedence. As the assignment operator (=) has the lowest precedence I see no reason why the two method should behave differently.

I also had a look at the IL generated:

.method public hidebysig static void Unexpected(class [System]System.Collections.Generic.Stack`1<class OperationOrder.Element> stack) cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: callvirt instance !0 [System]System.Collections.Generic.Stack`1<class OperationOrder.Element>::Peek()
    L_0006: ldarg.0 
    L_0007: callvirt instance !0 [System]System.Collections.Generic.Stack`1<class OperationOrder.Element>::Pop()
    L_000c: callvirt instance string OperationOrder.Element::get_Value()
    L_0011: callvirt instance void OperationOrder.Element::set_Value(string)
    L_0016: ret 
}

I’m not an IL crack, but for me this code still looks good ans should leave one element on the stack with value set to “two”.

Can anyone explain me the reason why the method Unexpected() does something different than Expected()?

Thanks a lot!

Lukas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:50:54+00:00Added an answer on May 13, 2026 at 4:50 pm

    Your expression is equivalent to

    stack.Peek().set_Value(stack.Pop().Value);
    

    When calling an instance method on a reference type, the instance is evaluated first.

    EDIT: As Eric Lippert points out, all expressions are evaluated left-to-right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is pretty basic stuff...you might want to start with… May 13, 2026 at 11:10 pm
  • Editorial Team
    Editorial Team added an answer [We wrote this] There are quite a few forks, which… May 13, 2026 at 11:10 pm
  • Editorial Team
    Editorial Team added an answer Try ReflectionClass ReflectionClass::getFileName — Gets a filename Example: class Foo… May 13, 2026 at 11:10 pm

Related Questions

I'm trying to implement a color picker in my Cocoa app. (Yes, I know
As I try to modernize my C++ skills, I keep encountering this situation where
I'm writing some custom Comparators, and I'd like them to push null items to
I was trying to use an STL list in C++ and I arrived into
So, I'm pulling my hair out here, and maybe someone has an insight. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.