How to Call a Non-Static Method from a Static Method in C#

Is there a way to call a non-static method from a static method?

Example() -> Example

You would just need to create an instance of the type then call the non-static, from a static method.

public class Example(){

public static void StaticExample()
{

Example example = new Example();
example.NonStatic();
}

public void NonStatic()
{

}

}

How to call a non static method in the main?

If a method can be made static without changing the code, you should make it static. To answer your question though, you can create an instance of Program in your Main function and call the non-static methods through it.

class Program
{
static void Main(string[] args)
{
var p = new Program();
p.bird();
}

public void bird()
{
birdSpeech();
}

public void birdSpeech()
{
Console.WriteLine("Chirp Chirp");
}
}

How do I call a non-static method from a static method from a different class?

You can not access non static context inside static context.

You can make _mainform static

static MainForm _mainform = null;

or pass the instance you want to test to Test method

public static void Test(MainForm mainForm)
{
mainForm.DoSmth("test");
}

or make Test non static

public void Test()
{
_mainform.DoSmth("test");
}

All aside you should revise your design. Think about these questions. If I have a static method why it should access an instance member? If a method should access instance members then why should it be static?

Call non-static method from static method c#

This is one method for calling a non-static method via a delegate. Note that it is a two-step process, since to call a non-static method, you absolutely need an instance of the class. I would also note that there is almost certainly a better way to do what you want to do, since needing to call a non-static method from a static method despite not wanting to use an object instance makes it sound like the non-static method should be static.

public class MyClass
{
private static Action NonStaticDelegate;

public void NonStaticMethod()
{
Console.WriteLine("Non-Static!");
}

public static void CaptureDelegate()
{
MyClass temp = new MyClass();
MyClass.NonStaticDelegate = new Action(temp.NonStaticMethod);
}

public static void RunNonStaticMethod()
{
if (MyClass.NonStaticDelegate != null)
{
// This will run the non-static method.
// Note that you still needed to create an instance beforehand
MyClass.NonStaticDelegate();
}
}
}

calling non static method from static method in console application

When you are working with DI container - all instantiations involving types from container (such as IFooService) should go through container.

In your case, Program depends on IFooService and so should also be registered in container:

public void RegisterTypes(IUnityContainer container)
{
//repository registration
container.RegisterType<IFooRepository, FooRepository>();

//services registration
container.RegisterType<IFooService, FooService>();

container.RegisterType<Program>();
}

And then resolved from container (which will also resolve all its dependencies, such as IFooService):

var unity = new UnityContainer();
var unityConfig = new MyUnityConfig();
unityConfig.RegisterTypes(unity);
var p = (Program)unity.GetService(typeof(Program));
p.MyFunc();

Why can't you call a non-static method from a static method?

A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method should operate on.

how to call static method inside non static method through instance

With the edit that this is talking about extension methods, then it becomes easier!

With the same example as below:

bool hasValue1 = s.HasValue(); // use the instance syntax
bool hasValue2 = StringExtensions.HasValue(s); // use the static syntax

Note that these are 100% identical; once compiled to IL you cannot determine which form was used.


Short answer: "no", per CS0176:

CS0176 Member '{name}' cannot be accessed with an instance reference; qualify it with a type name instead

Slightly longer answer: if you really want to do that, maybe extension methods are what you are looking for; for example:

static class StringExtensions
{
public static bool HasValue(this string value)
{
return !string.IsNullOrEmpty(value);
}
}
class Program
{
static void Main()
{
string s = /* some string or null reference */
bool hasValue = s.HasValue();
}
}


Related Topics



Leave a reply



Submit