Var keyword in Delphi/Oxygene and C#

I had the privilege of working on some of the interesting languages like Delphi, Oxygene, C# etc.

All the languages listed above has the keyword var which worked slightly different in each of the languages.

In Delphi and Oxygene, one can use var keyword to pass the parameter by reference as well as to start the block of variable initialization within the method.

method MainForm.Function1;

var

message1 : String;

begin

message1 := "Welcome to Ginktage.com";

load(var message1);

MessageBox.Show(message1);

end;

method MainForm.load(var data: String);

begin

data := "This is a test application";

end;

In C#, the var keyword denotes an anonymous type. When you want to assign a variable, you need not specify the type but instead specify it as var, C# finds out what is the resulting type. One of the most common use of the var keyword in C# is in the LINQ query.

Check the 5 Things That You Cannot do with a Local Type Inference In C# to know more about var keyword in C#

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply