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
?
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.
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.
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.
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 ...
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With