Introduction
To determine visibility area for temporary objective variable in current language versions specified using block construction as can be shown by ShowDialog
execution code fragment:
using namespace System.Windows.Forms;
using(var od = new OpenFileDialog())
{
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
}
However, in principle, it is possible to define alternative short syntax for anonymous objective variables.
Anonymous Objective Variables and Visibility Scopes
Direct access to just created anonymous objects is possible only if calling chain may be continued by method invocations which each time returns instance of object-caller. After actions are performed with it, this object occurs unreferenced and available for garbage collection, that is their visibility scope determined by this calling chain. If object required some scope for check and performs some action in current C# versions needed to refer the named variable:
using namespace System.Windows.Forms;
if((new OpenFileDialog()).ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
Keyword "last" as Alternative
Assume that visibility scope of last anonymous object reference unlimited until next anonymous reference occurs in the code or by default exists global reference variable "last
" to last created anonymous object which value replaces from time to time, then next another anonymous reference rewrites previous. Then ShowDialog
example can be represented in alternative short-syntax form with keyword "last
":
using namespace System.Windows.Forms;
if((new OpenFileDialog()).ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
last.Filter = "(*.*) | *.*";
// Some futher actions with last as last created ShowDialog
}
This feature requires some language modification to introduce keyword last
and generates extra post-compile code to support switching for last
and access by it.
Points of Interest
I guess that this syntax almost doesn't require platform changes so all code can be generated for existing versions of CLR. Such a method requires only C# to IL compile modification. But how much extra compile-time outlays it can cost, I will perhaps consider later.
History
- 6/12/2015: Initial version