Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - Is there any way to create a non-static thread method?

Is there any way to create a non-static thread method at .NET? Show me code please.

The following code doesn't work:

ThreadStart ts = delegate { drawFloorAround(); };
public void drawFloorAround()
{
            ...
}

Gives this error -> "A field initializer cannot reference the non-static field, method, or property". If I change the method do static, it works. But I don't want to.

like image 427
alansiqueira27 Avatar asked Dec 10 '10 17:12

alansiqueira27


People also ask

Can I make main method non static in C#?

The main method must be static, because static methods do not need an instance of the class in order to be called - all non static methods require an instance of the class to be created before you can call them.

How do I make a static method non static?

Convert a static method into an instance methodPress Ctrl+Shift+R and then choose Make Method Non-Static. Right-click and choose Refactor | Make Method Non-Static from the context menu.

Can we create non static method in static class?

Non-static variable and instance methods cannot be accessed within the static class. If you try to access a non-static reference from a static field, it throws an error: Cannot make a static reference to the non-static field. We can create static blocks, variables, and methods inside a static class.

How can call non static method from static method in asp net C#?

We can call non-static method from static method by creating instance of class belongs to method, eg) main() method is also static method and we can call non-static method from main() method . Even private methods can be called from static methods with class instance.


1 Answers

... gives this error "A field initializer cannot reference the non-static field, method, or property".

Read the error message more carefully. It is telling you precisely what is wrong. A field initializer cannot reference a non-static method. That's because the compiler is trying to protect you from this bug:

class C
{
    int foo;
    int bar = GetBar();
    public C(int newFoo)
    {
        this.foo = newFoo;
    }
    private int GetBar() { return this.foo + 1; }
}

You do "new C(123)". What is bar set to? If this were legal code then it would be set to 1, not 124. Why? Because first foo gets initialized to zero, then GetBar() gets called, then the constructor body sets this.foo to 123.

To prevent this bug it is simply illegal to reference an instance method or field in a field initializer.

Now, you might reasonably point out that in your code, you do not use the instance method, you only reference it. You never actually call it. This actually is safe. However, the rules of C# are designed to be simple and conservative; even though we could prove that this case is safe, we take the conservative, simple path and say that any reference to the instance in a field initializer is illegal.

If I change the method to static, it works.

Correct. In that case, the method does not depend upon instance state which has not yet been set up.

But I don't want to.

OK, then your only other choice is to stop using a field initializer. Put the initialization in the constructor; you then take responsibility for ensuring that the initialization does not accidentally use uninitialized state.

like image 146
Eric Lippert Avatar answered Oct 20 '22 11:10

Eric Lippert