Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Optional Parameter in C#

Optional Parameter in C#

Scheduled Pinned Locked Moved C#
csharpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arechno
    wrote on last edited by
    #1

    Hi all... Can there be an optional or default parameter in C#? If yes what would be the syntax of the same?

    C 1 Reply Last reply
    0
    • A arechno

      Hi all... Can there be an optional or default parameter in C#? If yes what would be the syntax of the same?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can define a params collection, otherwise you need to write several methods like this": void DoSomething(int i, int j, in k) { } void DoSomething(int i, int j) { DoSomething(i, j, 5); } void DoSomething(int i) { DoSomething(i, 2, 5); } Obviously, this is less than ideal. Christian Graus - Microsoft MVP - C++

      A 1 Reply Last reply
      0
      • C Christian Graus

        You can define a params collection, otherwise you need to write several methods like this": void DoSomething(int i, int j, in k) { } void DoSomething(int i, int j) { DoSomething(i, j, 5); } void DoSomething(int i) { DoSomething(i, 2, 5); } Obviously, this is less than ideal. Christian Graus - Microsoft MVP - C++

        A Offline
        A Offline
        arechno
        wrote on last edited by
        #3

        Ya...one option is of method overloading, but when we have more number of params to be kept as optional one has to type a lot. Is there any other work around as in VB.NET we have an optional keyword which specifies the parameter is optional and with the specified default value.

        C 1 Reply Last reply
        0
        • A arechno

          Ya...one option is of method overloading, but when we have more number of params to be kept as optional one has to type a lot. Is there any other work around as in VB.NET we have an optional keyword which specifies the parameter is optional and with the specified default value.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          To reiterate: no. I recently found out that VB.NET supports optional parameters and C# does not. Until then, I thought that VB.NET was always the loser, I guess that's not the case. Having said that, I think that an interface where a method takes 20 parameters and 10 are optional is just sloppy coding anyhow. Christian Graus - Microsoft MVP - C++

          D D 2 Replies Last reply
          0
          • C Christian Graus

            To reiterate: no. I recently found out that VB.NET supports optional parameters and C# does not. Until then, I thought that VB.NET was always the loser, I guess that's not the case. Having said that, I think that an interface where a method takes 20 parameters and 10 are optional is just sloppy coding anyhow. Christian Graus - Microsoft MVP - C++

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #5

            Here is entry about why C# doesn't have default parameters from C# Frequently Asked Questions. http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85556.aspx[^] Surprising perspective, isn't it? David

            1 Reply Last reply
            0
            • C Christian Graus

              To reiterate: no. I recently found out that VB.NET supports optional parameters and C# does not. Until then, I thought that VB.NET was always the loser, I guess that's not the case. Having said that, I think that an interface where a method takes 20 parameters and 10 are optional is just sloppy coding anyhow. Christian Graus - Microsoft MVP - C++

              D Offline
              D Offline
              Dave Doknjas
              wrote on last edited by
              #6

              C# vs VB is all a matter of style. If you look closely at the features of both languages, you'll see that VB offers about as many features that C# doesn't have that C# offers that VB doesn't have. C#: - allows unsafe code (I've yet to meet anyone who does this in C# though) - has the 'as' operator (but VB has it in 2005 - 'TryCast') - allows assignments within expressions (e.g., if ((x = y.Value) == 2)) - operator overloading VB: - has optional parameters - allows types within interfaces (although I've never wished I had this in C#) - has a very flexible Select construct (much, much more flexible than 'switch') - has the "When" filter for catch block headers (ok - I'm really stretching now...) And this one is a doubtful benefit of VB: it gives you half a dozen ways to do a lot of things - some regard it as a benefit, while others hate it since it makes it harder to produce standard code in teams. David Anton Tangible Software Solutions www.tangiblesoftwaresolutions.com Home of: Instant C#: VB.NET to C# Converter Instant VB: C# to VB.NET Converter Instant J#: VB.NET to J# Converter

              C 1 Reply Last reply
              0
              • D Dave Doknjas

                C# vs VB is all a matter of style. If you look closely at the features of both languages, you'll see that VB offers about as many features that C# doesn't have that C# offers that VB doesn't have. C#: - allows unsafe code (I've yet to meet anyone who does this in C# though) - has the 'as' operator (but VB has it in 2005 - 'TryCast') - allows assignments within expressions (e.g., if ((x = y.Value) == 2)) - operator overloading VB: - has optional parameters - allows types within interfaces (although I've never wished I had this in C#) - has a very flexible Select construct (much, much more flexible than 'switch') - has the "When" filter for catch block headers (ok - I'm really stretching now...) And this one is a doubtful benefit of VB: it gives you half a dozen ways to do a lot of things - some regard it as a benefit, while others hate it since it makes it harder to produce standard code in teams. David Anton Tangible Software Solutions www.tangiblesoftwaresolutions.com Home of: Instant C#: VB.NET to C# Converter Instant VB: C# to VB.NET Converter Instant J#: VB.NET to J# Converter

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Hi David. Have I bought your program ? Whichever VB->C# converter I bought, it truly rocked !!! David Anton wrote: allows unsafe code (I've yet to meet anyone who does this in C# though) You're talking to one right now. Vital for image processing, although I'm told there's a non-unsafe way, I am yet to see the code to know what it is ( setpixel does NOT count ). David Anton wrote: has the 'as' operator (but VB has it in 2005 - 'TryCast') Trouble is, most VB.NET users prefer to use the older casting operators, the ones that were in VB6. The problem with VB.NET is not lack of features, it's terrible hacks that have been maintained for legacy support ( and which Microsoft initally wanted to remove ). David Anton wrote: VB.NET : has optional parameters Yeah, these would be nice, although I read a rationale against them online which made sense. All about performance. David Anton wrote: And this one is a doubtful benefit of VB: it gives you half a dozen ways to do a lot of things - some regard it as a benefit, while others hate it since it makes it harder to produce standard code in teams. A problem that VB.NET only intensifies. It's my experience, from working on projects that are already in progress, that on average, VB.NET users know a hell of a lot less about good design and coding standards than C# users. It's also my experience that they don't use the new VB.NET features, because they are coding as if it was still VB6. I doubt many of them even know what it is they are asking the compiler to do. Whichever converter I bought, it won me a whole lot of work, because I was able to convert a huge VB.NET program to C#, and I have worked on it ever since. I agree, style is an issue, I hate VB.NET syntax, but there's more to it than that, and the core issue is the sort of people who are using each language, on average. I'm sure there are good VB.NET coders and bad C# coders in the world, but overall, the trend is in the other direction. Christian Graus - Microsoft MVP - C++

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups