473,789 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5328
100
Hi A.M,

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

B\rgds
100

"A.M" <IH*******@sapm 123.com> wrote in message
news:Oz******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP09.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*******@sapm 123.com> wrote in message
news:Oz******** ******@tk2msftn gp13.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.co m

"A.M" <IH*******@sapm 123.com> wrote in message
news:OJ******** ******@TK2MSFTN GP10.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******** ******@TK2MSFTN GP09.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*******@sapm 123.com> wrote in message
news:Oz******** ******@tk2msftn gp13.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.mi crosoft.com> wrote in message
news:sz******** ******@cpmsftng xa07.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
15576
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 = myList
18
7845
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 see no use of them. Others say thar they are bad. I like them. So, could anyone tell me why they are in the standard? Are they bad? I do not intend to start war. Nor am I a troll.
18
2254
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 continue to work.But I'v fogot how to use,even I'v fogot whether I can use it in C# or not. Help me.
7
4233
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
3046
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 this, if the code is jitted the "push xyz" instructions of the actual default values can be inserted. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself why one to...
4
12663
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 am trying to consume this web service from another asp.net application, only the default parameter-less constructor shows up. And if i try creating an instance of the second constructor, it gives me a compiler error saying that No overload of...
14
3275
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 make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
6
1467
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: class A: def __init__(self): self.x = 1 def meth1(self): return self.x
1
1015
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 value of another method of the same class. For example: class A: def __init__(self): self.x = 1
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.