473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Default method parameter

A.M
Hi,
Do we have default method parameter in C#?

Something like this

void method1(int i = 12)
{
....
}
Thanks,
Ali
Nov 15 '05 #1
7 5313
100
Hi A.M,

No, c# doesn't support default parameters. You can use method overloads for
that.

B\rgds
100

"A.M" <IH*******@sapm123.com> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
Hi,
Do we have default method parameter in C#?

Something like this

void method1(int i = 12)
{
...
}
Thanks,
Ali

Nov 15 '05 #2

"A.M" wrote...
Do we have default method parameter in C#?

Something like this

void method1(int i = 12)
{
...
}


No, but you can overload the method to gain the same result as if you had
default parameter values:

void method1(int i)
{
...
}

void method1()
{
method1(12);
}

// Bjorn A
Nov 15 '05 #3
A.M
But C# supposed to be a modern language and that method overloading you
mentioned can implicitly be done by compiler ,so what is the logic behind
this incapability ?

Thanks for replay,
Ali

"100" <10*@100.com> wrote in message
news:eZ**************@TK2MSFTNGP09.phx.gbl...
Hi A.M,

No, c# doesn't support default parameters. You can use method overloads for that.

B\rgds
100

"A.M" <IH*******@sapm123.com> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
Hi,
Do we have default method parameter in C#?

Something like this

void method1(int i = 12)
{
...
}
Thanks,
Ali


Nov 15 '05 #4
Ali,

I think that part of the reason for this is that overloads are a more
beneficial than default values. If you have default values, then you will
have to use the least common denominator with object types if you are going
to have multiple input types (kind of like you would have to use Object or
Variant in VB if you wanted to handle more than one type for the parameter
in your method). If you have default values and not overloads, then what
happens is you have to declare more of your methods with type-agnostic
parameters, and that's not a good thing.

Now, one can argue that you can have both overloads and default values.
So what do you do when you have the following:

public void DoSomething(int parameter = 0)

public void DoSomething()

Which one does it call? Regardless of what is actually done (or how C++
does it, because this is what it is what most answers are going to be based
on anyways), it adds confusion to the mix, and that is something I can only
guess the language designers didn't want to introduce to a new language.
Instead, they tend to take a wait-and-see approach.

Eric Gunnerson's blog entry today actually speaks about this to some
degree (watch for line wrap):

http://weblogs.asp.net/ericgu/archiv.../12/57985.aspx

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"A.M" <IH*******@sapm123.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
But C# supposed to be a modern language and that method overloading you
mentioned can implicitly be done by compiler ,so what is the logic behind
this incapability ?

Thanks for replay,
Ali

"100" <10*@100.com> wrote in message
news:eZ**************@TK2MSFTNGP09.phx.gbl...
Hi A.M,

No, c# doesn't support default parameters. You can use method overloads

for
that.

B\rgds
100

"A.M" <IH*******@sapm123.com> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
Hi,
Do we have default method parameter in C#?

Something like this

void method1(int i = 12)
{
...
}
Thanks,
Ali



Nov 15 '05 #5

Hi Ali,

I think Nicholas's reply is useful, does it make sense to you?
If you still have anything unclear, please feel free to tell me, I will
work with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #6
A.M
Yes, His reply answered my question.
Thanks Jeffrey.

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:sz**************@cpmsftngxa07.phx.gbl...

Hi Ali,

I think Nicholas's reply is useful, does it make sense to you?
If you still have anything unclear, please feel free to tell me, I will
work with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #7

Hi Ali,

Thanks for your feedback.
I am glad this group helps you. If you need further help, please feel free
to post, we will work with you.
Have a nice experience in Microsoft newsgroup!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Alex Panayotopoulos | last post by:
Hello all, Maybe I'm being foolish, but I just don't understand why the following code behaves as it does: - = - = - = - class listHolder: def __init__( self, myList= ): self.myList =...
18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
18
by: Clark Nu | last post by:
It seems that when I define a fuction,I can set a default value to some of the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then...
7
by: Vyssokih Max | last post by:
Hello! In C++, I can wrote: void Update(int count = 0) {...} and use it without parameters or with one parameter
8
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of...
4
by: indigator | last post by:
I have an ASP.Net web service class, DataLayer.asmx.cs. I have two constructors for the DataLayer class. One is the default parameter-less one and the second one accepts a string argument. When I...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
6
by: s0suk3 | last post by:
I wanted to know if there's any way to create a method that takes a default parameter, and that parameter's default value is the return value of another method of the same class. For example: ...
1
by: s0suk3 | last post by:
I had posted this before but all the spam whipped it out... I wanted to know if there's any way to create a method that takes a default parameter, and that parameter's default value is the return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.