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

  • SEARCH
  • Home
  • 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 6653111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:13:38+00:00 2026-05-26T01:13:38+00:00

Does anyone have any examples or ideas on how / what is the best

  • 0

Does anyone have any examples or ideas on how / what is the best way to implement a Stack class in C#? I understand that there is already a Stack class, but I need to understand how to actually implement a Stack class.

I also need advice on how to use Contracts in C# to specify preconditions, postconditions, and invariants for this class. I think I have used something similar before when creating models in the ASP.NET MVC architecture, but I’m not entirely sure if it is the same thing and works the same way. (I’m a bit lost on the preconditions/postconditions/invariants, if you couldn’t already tell – so please bear with me.)

My main question – could someone give me advice on properly using Contracts for a class such as a Stack.

Yes, I have laid out effort:

public interface IStack
{
        void Push(Object e);
        Object Pop();
        Object Top();
        void EnsureCapacity();
    }
}

   public class Stack : IStack
{
    private Object[] elements;
    private int size = 0;

    public Stack()
    {
        elements = new Object[0];
    }

    public void Push(Object e)
    {
        // check if this array capacity has been reached and increase if needed
        EnsureCapacity();
        elements[size++] = e;
    }

    public Object Pop()
    {
        // check if the method call is invalid for the object's current state
        if (size == 0) throw new InvalidOperationException("Stack.Pop");

        Object result = elements[--size];
        elements[size] = null;

        return result;
    }

    public Object Top()
    {
        // check if the method call is invalid for the object's current state
        if (size == 0) throw new InvalidOperationException("Stack.top");
        return elements[(size - 1)];
    }

    private void EnsureCapacity()
    {
        if (elements.Length == size)
        {
            Object[] oldElements = elements;
            elements = new Object[(2 * size + 1)];
        }
    }
}
  • 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-26T01:13:38+00:00Added an answer on May 26, 2026 at 1:13 am

    If you want, for getting started using Microsoft Code Contracts, I made a blog post about it once. That post covers the very basic of preconditions, post-conditions, and invariants.

    As a summary of the concepts, you can think of them as follows:

    • Precondition is what must be true prior to a method being executed — what clients promise your method.
    • Invariant is what must remain publicly true at all times as far as clients of your class are concerned.
    • Postcondition is what must be true following a method execution — what your method promises to clients.

    So, off the top of my head, for a stack, an easy thing to think of might be an invariant. If you’re modeling the stack with an array, you might declare an invariant on the class that the array is never set to null, for example you’d define the invariant method:

    [ContractInvariantMethod]
    private void ObjectInvariant()
    {
       Contract.Invariant(elements != null);
    }   
    

    It looks like you’ve already got a precondition on your pop method – you want to say that it’s incumbent on the user to make sure that the stack is not empty when he executes a pop. So, at the beginning of the pop method, you’d have:

    Contract.Requires(size > 0);
    

    And finally, you might specifiy a post-condition on pop, that size will always be less than it was before the pop operation (you could get more specific if you like):

    Contract.Ensures(Contract.OldValue<int>(size) > size);
    

    Good luck with it — contracts are cool and useful. It’s a very clean way to code.

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

Sidebar

Related Questions

Does anyone have any ideas what the best way to make a plugin system
Does anyone have any code examples on how to create controllers that have parameters
Does anyone have any examples or resources where i might find information on scrolling
Does anyone have any examples of using Sqlite with ASP.NET membership? I am building
does anyone have any examples of using NSKeyedArchiver in monotouch? i just want to
Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone have any ideas on if/how one could create a regular expression to
Does anyone know of any examples or tutorials of an MVC view that shows
Does anyone have any examples of starting the file stream before the rendering/building process

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.