Click here to Skip to main content
15,886,519 members
Articles / Desktop Programming / Win32
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.70/5 (10 votes)
9 Dec 2015CPOL1 min read 15.7K   2   10
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice try, but not needed ... Pin
mbb0115-Dec-15 5:02
mbb0115-Dec-15 5:02 
AnswerRe: Nice try, but not needed ... Pin
U_IO9-Jan-16 6:42
U_IO9-Jan-16 6:42 
Suggestionnot a good idea... Pin
John Torjo10-Dec-15 11:59
professionalJohn Torjo10-Dec-15 11:59 
GeneralRe: not a good idea... Pin
U_IO10-Dec-15 22:23
U_IO10-Dec-15 22:23 
GeneralRe: not a good idea... Pin
John Torjo15-Dec-15 20:49
professionalJohn Torjo15-Dec-15 20:49 
GeneralRe: not a good idea... Pin
U_IO21-Dec-15 23:55
U_IO21-Dec-15 23:55 
QuestionГде описано? Pin
small_bob7-Dec-15 23:48
small_bob7-Dec-15 23:48 
AnswerRe: Где описано? Pin
U_IO10-Dec-15 22:13
U_IO10-Dec-15 22:13 
GeneralMy vote of 4 Pin
Manas_Kumar6-Dec-15 17:42
professionalManas_Kumar6-Dec-15 17:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.