This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member-for instance, a method or a property-on a variable that currently holds a null reference.īut what is a null reference? What are “references” in the first place? How can you stop the NullReferenceException from happening in your code? That’s what we’re going to cover in today’s post. We’ll start with fundamentals, by giving a brief explanation of what references are in C#/.NET. After that, you’ll learn what null references are. At this point, you’re halfway there to seeing the whole picture.Īfter this round of theoretical definitions, we’ll get to more practical matters, teaching you how to avoid the NullReferenceException in practice. But what is a null reference? In what way does it differ from a non-null reference? We already know that the NullReferenceException is caused by a null reference. NET, you can divide data types in two categories: value types and reference types. If you have a variable of a value type, it stores the value itself. Reference types variables, on the other hand, don’t hold the value itself. They hold a reference that points to where the object lives in memory.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |