© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
22 replies
Faker

✅ NullReferenceException when declaring an array but not initializing it

Hello guys, say I created a new class and within that new class, I created an array.
So we have something like this:

class MyClass
{
  private int[] numbers;
  private string someVariable;

  public MyClass() // default constructor
{
  someVariable = "";
}
class MyClass
{
  private int[] numbers;
  private string someVariable;

  public MyClass() // default constructor
{
  someVariable = "";
}

Noticed that we didn't initialize numbers array. So what will happen if we try to access it? we get a NullReferenceException? If so, what is happening internally pls. Like when we declare
int[] numbers
int[] numbers
, normally numbers should store a reference to where the array is on the heap, right? But since their is no "array" yet, numbers points to nothing, so to null?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Json Deserialiser throws NullReferenceException when deserializing an array
C#CC# / help
3y ago
{} vs [] when initializing an array in C#
C#CC# / help
13mo ago
❔ Initializing an object
C#CC# / help
3y ago
NullReferenceException but checking for Null?
C#CC# / help
2y ago