codebrothers.net :: Home  >  Matthias Denkmaier
User: Guest  Login
Matthias Denkmaier
C# - what else?
Blog search:  
How to define optional C# method parameters with [Optional] attribute

One feature that i am missing when using C# is the ability to declare a method parameter as optional.

If you have to call a specific method without having enough input values to meet the exact signature you can use the following way:

 

object m = Type.Missing;

object url = "http://www.microsoft.com";

InternetExplorer ie = new InternetExplorer();

ie.Navigate2(ref url,ref m,ref m,ref m,ref m);

ie.Visible = true;

 

If you have already existsing VB.NET code calling methods in your C# assemblies you can provide a way of declaring optional parameters by using the [Optional] attribute:

public void Demo( string text, [Optional] string _text2 )...

Note: When you call the same method signature from C# code you have to provide all parameter values, the optional flag is only supported and defined by VB.NET code.

Share this post: Email it!
Posted:  3/9/2007 7:08:32 PM  by  mdenkmaier
Tags:  How toC#Development
No comments, yet!
Leave a Comment

Name required

Your URL

Comments (required)