C# Error CS0572 – ‘type’ : cannot reference a type through an expression; try ‘path_to_type’ instead

C# Compiler Error

CS0572 – ‘type’ : cannot reference a type through an expression; try ‘path_to_type’ instead

Reason for the Error

You will get this error in your C# code when you are trying to access the member of a class through an identifier that is not permitted.

For example, let’s compile the below C# program

using System;

namespace DeveloperPublishConsoleCore
{
    class Parent
    {
        public class InnerParent
        {
            public static int ID = 100;
        }
    }
    class Child : Parent
    {
        public void WriteData()
        {
            Parent obj = new Parent();
            obj.InnerParent.ID = 100;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

You will receive the error code CS0572 because you are trying to access the member (ID) of the InnerParent class through the identifier.

Error CS0572 ‘InnerParent’: cannot reference a type through an expression; try ‘Parent.InnerParent’ instead DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 17 Active

C# Error CS0572 – 'type' : cannot reference a type through an expression; try 'path_to_type' instead

Solution

You can fix this error by ensuring that you are trying to access the member in the right way. For example, the error in the above code can be fixed by calling Parent.InnerParent.ID = 10.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...