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 3323490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:18:31+00:00 2026-05-17T23:18:31+00:00

Say I have this C# class: public class HttpContextEx { public HttpContext context =

  • 0

Say I have this C# class:

public class HttpContextEx
{
    public HttpContext context = null;
    public HttpRequest req = null;
    public HttpResponse res = null;
}

How do I declare an object of it, inside a function, which will be allocated on the stack and not on the heap?
In other words I want to avoid using the ‘new’ keyword for this one. This code is bad:

HttpContextEx ctx = new HttpContextEx(); // << allocates on the heap!

I know what stack/heap are perfectly and I’ve heard of the wonderful C# GC, yet I insist to allocate this tiny object, which is here only for convenience, on the stack.

This attitude comes from C++ (my main tool) so I can’t ignore this, I mean it really ruins the fun for me here (:

  • 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-17T23:18:32+00:00Added an answer on May 17, 2026 at 11:18 pm

    If you changed it to a value type using struct and create a new instance within the body of a method, it will create it on the stack. However the members, as they are reference types will still be on the Heap. The language whether it be a value or a reference type will still require the new operator but you can use var to eliminate the double use of the type name

    var ctx = new HttpContextEx(); 
    

    Otherwise, take C# as it is since the GC does a great job.

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

Sidebar

Related Questions

Let's say I have this simple structure class FooDefinition { public FooDefinition Parent {
Let's say I have two classes that look like this: public class ByteFilter {
I have this class, let's say, Foo . It extends JFrame and is a
Let's say I have swing app and I am using mybatis: public class MyAppCView
I have a class that takes a bit of time to start up (makes
This question is a continuation of this one but asking for a more specific
I have been looking for help online on how to design my php classes
By default non-static methods have their own instance of variables for each thread when
First, a little background. I have two ASP.NET web applications that use SQLServer session
I am wondering if there is any difference in runtime between a generic container

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.