Use of unassigned local variable

Posted on 2005-04-22
Last Modified: 2010-04-16
Hello to all experts! :o)

In a project, I have created my own class to make siblings-objects. A sibling-object shall only be created if the childArray has elements. Why do I get the error "Use of unassigned local variable children" in the writeline-line?

            public static void Main(){
                  string[] childArray = {"child 1", "child 2"};

                  wSiblings children;

                  if (childArray.Length > 0){
                        children = new wSiblings();
                        children.Id = 1;


What am I missing or have forgotten about c#-programming?

Question by:Halkatla
    1 Comment
    LVL 2

    Accepted Solution

    Hi Halkatla,

    If childArray.Length was == 0, then children would not have been set to anything, so you the compiler complains to you.

    If you added

    children = null;

    for example, (either outside the if, or in an else to it), then children would always be assigned to, so the compiler wouldn't complain.

    Hope this helps,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now