Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Alternative Short Syntax For Last Anonymous Objective Variables Scope in C#

0.00/5 (No votes)
9 Dec 2015 1  
Consider possibility of alternative syntax usage to refer and access to last anonymous objective variables in C# instead of using construction in code.

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)
            {
                // TO DO something futher    
            }
        }

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)
   {
       // Reference to anonymous ShowDialog out of scope     
   }

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here