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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:06:30+00:00 2026-06-11T07:06:30+00:00

I want to check if a variable is initialized at run time, programmatically. To

  • 0

I want to check if a variable is initialized at run time, programmatically. To make the reasons for this less mysterious, please see the following incomplete code:

string s;

if (someCondition) s = someValue;
if (someOtherCondition) s = someOtherValue;

bool sIsUninitialized = /* assign value correctly */;

if (!sIsUninitialized) Console.WriteLine(s) else throw new Exception("Please initialize s.");

And complete the relevant bit.

One hacky solution is to initialize s with a default value:

string s = "zanzibar";

And then check if it changed:

bool sIsUninitialized = s == "zanzibar";

However, what if someValue or someOtherValue happen to be “zanzibar” as well? Then I have a bug. Any better way?

  • 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-11T07:06:32+00:00Added an answer on June 11, 2026 at 7:06 am

    Code won’t even compile if the compiler knows a variable hasn’t been initialized.

    string s;
    if (condition) s = "test";
    // compiler error here: use of unassigned local variable 's'
    if (s == null) Console.Writeline("uninitialized");
    

    In other cases you could use the default keyword if a variable may not have been initialized. For example, in the following case:

    class X
    { 
        private string s;
        public void Y()
        {
            Console.WriteLine(s == default(string));  // this evaluates to true
        }
    }
    

    The documentation states that default(T) will give null for reference types, and 0 for value types. So as pointed out in the comments, this is really just the same as checking for null.


    This all obscures the fact that you should really initialize variables, to null or whatever, when they are first declared.

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

Sidebar

Related Questions

I want to repeatedly check a variable every time php wakes up from sleep().
I want to check a variable in ruby to see if it has two
I want to check if a variable exists. Now I'm doing something like this:
I want to check if a variable is null. If it is null, then
My question is I want to check a variable in a xml variable called
I want to check a char variable is one of 21 specific chars, what
I want to check if a variable has a valid year using a regular
I want to check if a variable has a number in it, I just
I want to check this: if ( typeof myVar != undefined && myVar !=
I want to check a variable value before a div so that if variable

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.