The Future of Managed Languages: C# and Visual Basic, Page 2
Dynamically Typed Objects
It is not uncommon to have to interact with objectsdifferently depending upon the source technology. Thefollowing example code depicts the differences in calling asimple calculator that is based on managed code, COM, orJavaScript. You will see the syntax is very different toaccomplish the same thing.
// Managed code Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); // COM Interop object calc = GetCalculator(); Type calcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 }); int sum = Convert.ToInt32(res); // JavaScript ScriptObject calc = GetCalculator(); object res = calc.Invoke("Add", 10, 20); int sum = Convert.ToInt32(res);The next release of both C# and Visual Basic willintroduce a new dynamic type. The name can be concerning tofolks, but rest assured it is actually a static type. Ratherthan having member selection and type selection at compiletime it is deferred until run-time. It behaves similarly tonormal static types in other ways. The example code belowdemonstrates the use of the dynamic type.
// Method chosen at compile time double x = 1.75; double y = Math.Abs(x); // Method double Abs(double x) chosen at run-time dynamic x = 1.75; dynamic y = Math.Abs(x); // Method int Abs(int x) chosen at run-time dynamic x = 2; dynamic y = Math.Abs(x);
Optional and Named Parameters
Optional and named parameters are items being introducedin to C# that gives it more parity with Visual Basic.Optional and named parameters have existed in Visual Basicfor as long as I can remember and will now be in C# aswell.
// Optional parameters have default values public StreamReader OpenTextFile( string path, Encoding encoding = null, bool detectEncoding = true, int bufferSize = 1024); // Leaving off parameters in the call OpenTextFile("foo.txt", Encoding.UTF8); // Named parameters can appear in any order OpenTextFile( bufferSize: 4096, path: "foo.txt", detectEncoding: false);Improved COM Interoperability
The object binders introduced with the DLR along with theintroduction of optional and named parameters provides abetter COM interoperability experience overall.
// Original code object fileName = "Test.docx"; object missing = System.Reflection.Missing.Value; doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); // Replacement code now possible doc.SaveAs("Test.docx"); Future Directions for Visual Basic
Much of the language innovations currently planned for VB10 focus around the aforementioned parity. The list containsbut is not limited to the following:
- Generate from use - type an object name and allow the IDE to generate a stub object or method.
- Auto-implemented properties - create a public property that is not backed by a full accessor method or field and the compiler will generate one.
- List initializers - ability to create an initialize a list all within a single statement the same as what C# supports.
- Implicit line continuation - do not have to use the annoying "_" in order to continue a line to the next line!
- Parallel extensions - extensions added to support parallel programming.
- No primary interop assembly - ability to embed referenced types directly within an assembly.
Summary
You have learned about a shared goal between C# andVisual Basic as well as some of the industry trends that areinfluencing the next release of the two languages. We lookedspecifically at some of the items planned for bothlanguages. I hope that it provided you high-level insight asto what is coming.
Future Columns
The topic of the next column is likely to be a deeperdive in to one of the topics on the next release of the .NETFramework. If you have something in particular that youwould like to see explained here you could reach me at
About the Author
Mark Strawmyer is a Senior Architect of .NET applicationsfor large and mid-size organizations. Mark is a technologyleader with CroweHorwath LLP in Indianapolis, Indiana. He specializes inarchitecture, design and development of Microsoft-basedsolutions. Mark was once again honored to be named aMicrosoft MVP for application development with C#. You canreach Mark at
