开发者

error: Object reference not initialised

开发者 https://www.devze.com 2023-01-21 17:49 出处:网络
I have the following code in C#: IList<string> myList = null; myList.Add(temp); temp is a string that is decalred elsewhere and is not null (I checked it). I keep getting the following error at t

I have the following code in C#:

IList<string> myList = null;

myList.Add(temp);

temp is a string that is decalred elsewhere and is not null (I checked it). I keep getting the following error at the line myList.Add(temp); "Object reference not initialised to an instance of an object"

What am I doing wrong here???

Updating the quesiton: I have already tried new

IList<string> myList = new List<string>();

as most of you suggested andget the following from Intellisense:

Cannot create an instance of the abstract class or interface Systems.Collections.Generic.Ilist.

Thanks for your previous answers. Now I am running into a peculiar problem. My datareader has an empty string in it. (1 field in the sql server table is blank. that is string.Empty). Is there a way开发者_运维技巧 to get rid of this inside the reader (I mean rdr here)??


you need to initialize the list first:

IList<string> myList = new List<string>();
...

Please note that on the right of = you have to write List<string> not IList<string> since IList<string> is an interface, whereas List<string> is a class that implements that interface.


The line:

IList<string> myList = null;

does not give you a list, but an empty reference to where a list could be.

IList<string> myList = new List<string>();

would properly instantiate myList, so you can use it (Add, Remove, etc.).


Here, take a look at this tutorial to better understand variable initialization.

From http://www.csharphelp.com/2007/03/objects-classes-in-c/

Person Michael = new Person();

In the first line of code we specified integer variablecalled age. In the second line we specified first the type of Object we need tocreate followed by the object’s name followed by a reserved operator called new and we end by typing the class name again followed byparenthesis “()”.

Let’s understand it step-by-step. Specifying the class nameat the beginning tell the C# Compiler to allocate a memory location for thattype (C# compiler knows all the variables and properties and methods of theclass so it will allocate the right amount of memory). Then we followed theclass name by out object variable name that we want it. The rest of the code”=new Person();” callthe object’s constructor. We will talk about constructor later but for nowunderstand that the constructor is a way to initialize your object’s variablewhile you are creating it not after you create it. For example, The Michaelobject we created it in the last section can be written as following :

Person Michael = new Person(20, “Brown”);

here I specified the variable’s values in the parameter listso I initialized the variables while I’m creating the object. But for this codeto work we will need to specify the constructor in the Person class and I willnot do that here because constructor section will come in later articles. Ithink you got a good introduction about Classes and Objects not I will completein in my next article and I will talk about constructors and building blockscoping. I hope you got a new thing from my article.


Updating the quesiton: I have already tried new Ilist() as most of you suggested andget the following from Intellisense: Cannot create an instance of the abstract class or interface Systems.Collections.Generic.Ilist

You cannot create instances of an Interface. IList<T> is an interface. Like the others have said here you initialize it with an instance of a concrete class that inherits the interface. Notice how they use new List<String>() not new IList<String>();

IList<String> items = new List<String>();
0

精彩评论

暂无评论...
验证码 换一张
取 消