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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:05:28+00:00 2026-06-03T10:05:28+00:00

I often see in C# code the following convention: some_type val; val = something;

  • 0

I often see in C# code the following convention:

some_type val;
val = something;

like

DataTable dt;
dt = some_function_returning_datatable();

or

DataTable dt = new DataTable();
dt = some_function_returning_datatable();

instead of

some_type val = something;
DataTable dt = some_function_returning_datatable();

I initially assumed that this was a habit left over from the days when you had to declare all local variables at the top of the scope. But I’ve learned not to dismiss so quickly the habits of veteran developers.

(In my 3rd code section will it not be wastage of memory when we assign dt first with new and then from function)

So, is there a good reason for declaring in one line, and assigning afterwards?

  • 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-06-03T10:05:30+00:00Added an answer on June 3, 2026 at 10:05 am

    In my 3rd code section will it not be wastage of memory when we assign dt first with new and then from function

    Yes, it will indeed. Only relatively minor – creating a useless DataTable object – but still wasteful and unclear.

    So, is there a good reason for declaring in one line, and assigning afterwards?

    Only if you don’t have the value immediately. For example:

    string x;
    if (someCondition) {
        // Do some work
        x = someResult;
    } else {
        // Do some other work
        x = someOtherResult;
    }
    

    Often this can be improved either using a conditional operator or extracting that code into a method. Sometimes it doesn’t work out that way though.

    For simple situations of:

    Foo x = SomeInitializationWithNoUsefulSideEffects();
    x = SomeUsefulValue();
    

    Or

    Foo x;
    x = SomeUsefulValue();
    

    You should absolutely refactor to

    Foo x = SomeUsefulValue();
    

    One other interesting situation where the declaration point does make a difference is in captured variables, although normally not the way it’s intended:

    int x;
    for (int i = 0; i < 10; i++) {
        x = SomeFunction();
        actions.Add(() => Console.WriteLine(x));
    }
    

    vs

    for (int i = 0; i < 10; i++) {
        int x = SomeFunction();
        actions.Add(() => Console.WriteLine(x));
    }
    

    In the first snippet, each delegate will capture the same variable, so they would all effectively see the last value returned from SomeFunction. In the second snippet, each delegate will capture a separate “instance” of x.

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

Sidebar

Related Questions

I quite often see the following naming convention used in java code. class SomeClass
I often see code like int hashCode(){ return a^b; } Why XOR?
Question 1: I am studying Android service and often see code like this: private
Often I see javascript code where event handlers (like onmousemove) are assigned dynamically. Example:
I often see code such as the following when, e.g., representing a large bitmap
I often see the following code: (function () { // init part })(); but
I often see a pattern used in circumstances where we have look-up code that
I have an example of some code that I see often in websites that
So in the following bit of code, I really like the verbosity of scenario
I often see questions like this one and there are multiple solutions. I'm trying

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.