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

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...