Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
List<string> _otherList = new List<string>()
                        {
                            "carrot",
                            "banana"
                        };


In the above code I initialize a list exactly the same way i found this online.i mean some one put this one online. but its showing errors like ; expected why?

My intention is that I want to have this newlist to have some initial value so that I can add things on it by invoking its add method on it,so that it wont throw a null exeption.


So what do I do to invoke this list with its add method( _otherList.add) in a way that this empty or null list is not getting called when i call its add method.
Posted
Updated 9-Nov-11 8:38am
v3

1 solution

I'm not sure I understand your question, but all you have to do to avoid the null exception is initialize the list:

C#
public class MyClass
{
    private List<string> _otherList = new List<string>();

    public MyClass()
    {
    }
}


After that, you can call _otherList.Add() whenever you want to.
 
Share this answer
 
Comments
rajh7 9-Nov-11 14:38pm    
Thanks it helped

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900