I have wrote the code snippet on dotnetfiddle.net with compiler settings set to .NET 7
using System;
public class Person
{
private string fname;
private string lname;
private int ide = 30;
public Person(string fN, string lN)
{
fname = fN;
lname = lN;
}
public bool ID() => GetType().GetProperty("ide").GetValue(this, null).Equals(30);
}
public class Program
{
public static void Main()
{
Person p = new Person("Mandy", "Dejesus");
p.ID();
}
}
I have checked others questions who have received the same warning and they seem to suggest creating an object with the new keyword. I've done that yet I still get the error. What am I doing wrong?
I expect true
but I'm currently getting
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object
at Person.ID()
at Program.Main()