chikitin
chikitin

Reputation: 781

How to Reference .Net Core Library in a .Net Core Console Application

I am following this code example

I am on Visual Studio Community 2019 for Mac. I created a .Net Core - Class Library project and compiled to create the assembly file P1-ProgramStructure.dll.

I created another solution with program2.cs code. Please see the code below. I renamed the .dll to acme.dll and copied the file into its directory.

Class library - .Net Core Project

Program1.cs

using System;
namespace Acme.Collections
{
    public class Stack
    {
        Entry top;
        public void Push(object data) 
        {
            top = new Entry(top, data);
        }

        public object Pop() 
        {
            if (top == null)
            {
                throw new InvalidOperationException();
            }
            object result = top.data;
            top = top.next;
            return result;
        }

        class Entry
        {
            public Entry next;
            public object data;
            public Entry(Entry next, object data)
            {
                this.next = next;
                this.data = data;
            }
        }
    }
}

.Net Core Console App

Program2.cs

using System;
using Acme.Collections;
class Example
{
    static void Main() 
    {
        Stack s = new Stack();
        s.Push(1);
        s.Push(10);
        s.Push(100);
        Console.WriteLine(s.Pop());
        Console.WriteLine(s.Pop());
        Console.WriteLine(s.Pop());
    }
}

When I run the project, I get the error:

$ dotnet run

Program.cs(15,7): error CS0246: The type or namespace name 'Acme' could not be found (are you missing a using directive or an assembly reference?) [/Users/csarami/VisStudioProjects/cSharp Projects/Project2-ProjectStructure/Project2-ProjectStructure/Project2-ProjectStructure.csproj]

The build failed. Please fix the build errors and run again.

Upvotes: 0

Views: 1607

Answers (1)

Learner
Learner

Reputation: 447

Make sure both projects have the same target framework

Upvotes: 1

Related Questions