Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this in static context?

Tags:

c#

public T Prop = new Ctor(Method);

private K Method(U controller, V request);

I get that it can't access Method here, because Method is required to be static. Why is this? Prop is not static.

Update: Here's the actual signature:

public DataSource(Func<ControllerBase, AjaxDataTable.Request, Result> dataSelector)

And this is how the method used to be:

public AjaxDataTable<SourcesViewModel.Source.Channel>.DataSource AjaxData =
    new AjaxDataTable<SourcesViewModel.Source.Channel>.DataSource(OnSelectData);

This wasn't an issue because I didn't need to reference this, so when I added this and it didn't compile anymore, (the field wasn't mine to begin with, someone else had done it like that, so don't hate me for that). I changed it to the following:

private AjaxDataTable<SourcesViewModel.Source.Channel>.DataSource ajaxData;
public AjaxDataTable<SourcesViewModel.Source.Channel>.DataSource AjaxData
{
    get
    {
        if (ajaxData == null)
        {
            ajaxData =
                new AjaxDataTable<SourcesViewModel.Source.Channel>
                    .DataSource(OnDataSelector);
        }
        return ajaxData;
    }
}

But then the ajaxData private field would always be null, which makes the data table be instanced again every time (this is appended into a concurrent dictionary, which causes an Exception on a duplicate key). I want to know how assigning to a field that doesn't reference this works. Does that create something that is reused across all instances of an object?

like image 692
bevacqua Avatar asked Jan 09 '12 13:01

bevacqua


People also ask

Why can't we use this inside static context?

No, we can't use “this” keyword inside a static method. “this” refers to current instance of the class. But if we define a method as static , class instance will not have access to it, only CLR executes that block of code. Hence we can't use “this” keyword inside static method.

Why this is not used in static method?

The "this" keyword is used as a reference to an instance. Since the static methods doesn't have (belong to) any instance you cannot use the "this" reference within a static method.

What is meant by static context?

Static context. The static context is used to configure prepare-time characteristics. Note: Prepare-time refers to the execution of one of the prepare methods on XFactory or the execution of one of the compile methods on XCompilationFactory.

Can we use this and super in static context?

We cannot use this and super keyword in the body of the static method because this keyword is the reference of the current object but without creating an object we can access the static method this will cause an error and in the case of the super keyword, we know very well super is the reference of the parent class but ...


1 Answers

This is due to section 10.5.5.2 of the C# 4 spec, which includes:

A variable initializer for an instance field cannot reference the instance being created. Thus it is a compile-time error to reference this in a variable initializer [...]

You're effectively referencing this, as your code is equivalent to:

public T Prop = new Ctor(this.Method);

Now you can argue that this restriction is draconian, but that's a different discussion. Note that you could put it into a constructor body:

public T Prop;

public Foo()
{
    Prop = new Ctor(Method);
}

(I'm assuming you wouldn't really have a public field, and that you'd have sensible names, etc :)

like image 57
Jon Skeet Avatar answered Sep 22 '22 02:09

Jon Skeet