Trying to build and use class library in C#.
Creating class library: File->New Project->Windows->Classic Desktop->Class Library Code:
namespace ClassLibrary2
{
public class Class1
{
public static long Add(long i, long j)
{
return (i + j);
}
}
}
Trying to consume it from console application:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ClassLibrary2.Class1 c = new Class1();
c. //no Add function
}
}
}
But c
object not contains Add
function. Why? How how to fix it?
Add
is a static method. You can't call static methods "via" instances in C#. That has nothing to do with it being in a different library.
You can call the method as:
long result = ClassLibrary2.Class1.Add(10, 20);
or if you actually have a using
directive for ClassLibrary2
(it's unclear from the question):
long result = Class1.Add(10L, 20L);
Alternatively, change the method to be an instance method, if that's what you wanted - at which point you'd be able to call c.Add(10L, 20L)
.
See more on this question at Stackoverflow