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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:36:46+00:00 2026-05-16T01:36:46+00:00

In C#, would there be any difference in performance when comparing the following THREE

  • 0

In C#, would there be any difference in performance when comparing the following THREE alternatives?

ONE

void ONE(int x) {

if (x == 10) 
{
    int y = 20;
    int z = 30;
    // do other stuff
} else {
    // do other stuff
}
}

TWO

void TWO(int x) {

int y;
int z;

if (x == 10) 
{
    y = 20;
    z = 30;
    // do other stuff
} else {
    // do other stuff
}
}

THREE

void THREE(int x) {

int y = 20;
int z = 30;

if (x == 10) 
{
    // do other stuff
} else {
    // do other stuff
}
}
  • 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-16T01:36:47+00:00Added an answer on May 16, 2026 at 1:36 am

    All else being equal (and they usually aren’t, which is why you normally have to actually test it), ONE() and TWO() should generate the same IL instructions since local variables end up scoped to the whole method. THREE() will be negligibly slower if x==10 since the other two won’t bother to store the values in the local variables.

    All three take up the same amount of memory—the memory for all variables is allocated even if nothing is stored in them. The JIT compiler may perform an optimization here, though, if it ever looks for unused variables.

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

Sidebar

Related Questions

I would like to know is there any difference in performance between these two
I was wondering if there is any difference in performance (or any other important
Is there any difference performance-wise in the following 2 snippets of code? NSString* str
Is there any performance difference (in C++) between the two styles of writing if-else,
Is there any performance difference between these two approaches? // First approach, iterating until
Is there any performance difference between dbms_lob.instr and contains or am I doing something
Is there any significant difference in performance between Canvas and XNA? What I mean:
I was wondering if there is any difference in performance when you compare/contrast A)
Is there any performance difference between using two separate calls to hide two elements,
Regarding performance, is there any difference between doing: $message = The request $request has

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.