Support for Named and Optional Parameters
Support for Optional parameters, default values and named parameters are new interesting features added to C# 4.0. You now no longer need to bother about your parameters in method calls - they have now been made much simpler. Consider the following method:
public static int AddValues(int x = 5, int y = 10) { return (x + y); }
You can now call this method AddValues() as simply as the following call shows you to:
int myValue = AddValues(); //The myValue integer variable would now have a value of 15.
Refer to the code snippet above. As you can see, the default values assigned to the parameters in the AddValues() method are 5 and 10. So, even if you don't pass any value when invoking the method, these values would be considered.
Enhanced COM Interop Features
C# 4.0 now allows you to have better flexibility in writing COM interoperable code - it is now much easier to call COM objects from your .NET code. Refer to the following code sample that shows how you can open a Microsoft Word Document in C#.
using Word = Microsoft.Office.Interop.Word;
namespace PCQ { class Program { static void Main(string[] args) { Word.Application wordApplication = new Word.Application() { Visible = true }; object missingValue = System.Reflection.Missing.Value; object readOnlyValue = true; object fileName = @"E:\\PCQ.docx"; wordApplication.Documents.Open(ref fileName, ref missingValue, ref readOnlyValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,ref missingValue); } } }
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.