0
Class StudentFeeCollection
{
public static bool CheckAdmissionMonth(int AdmissionNo)
 {
 }
public static DataTable CheckAdmissionMonth(int AdmissionNo)
 {
 }
}

Is this possible or not, please tell me.

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
  • For future reference, the compiler in your IDE could have told you this. Try to do a little exploring on your own before looking for someone to tell you--it will aid your learning greatly! – Brian Warshaw Feb 06 '14 at 12:29

4 Answers4

1

You can use out parameter:

 class StudentFeeCollection
{
public static void CheckAdmissionMonth(int AdmissionNo, out bool result)
{
    ........
}
public static void CheckAdmissionMonth(int AdmissionNo, out DataTable tbl)
{
    .......
}
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
0

No, that's not possible. You need to make sure that the signature of each overload is unique.

From the documentation:

Changing the return type of a method does not make the method unique as stated 
in the common language runtime specification. You cannot define overloads that 
vary only by return type.

Reference: http://msdn.microsoft.com/en-us/library/vstudio/ms229029(v=vs.100).aspx

Kenneth
  • 28,294
  • 6
  • 61
  • 84
0

This is not possible. Imagine you're the compiler or the runtime--how would you know which return type the code was asking for? If you really need to support returning multiple datatypes from a method, using generics is your best bet. That said, looking at your specific example, I suggest not doing it here. Having one method that returns either a boolean or a DataTable seems like a pretty shoddy design.

Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
0

You can overload by argument types in c# but not by return type. As Arshad said you can use out/ref parameters since these are arguments and not return types.

Also you can't overload by generic constraints of arguments(say having 2 versions where one is a struct and another is a class). see https://msmvps.com/blogs/jon_skeet/archive/2010/10/28/overloading-and-generic-constraints.aspx

one reason to avoid return type overloading, from the c++ language description:

The reason is to keep resolution for an individual operator or function call context-independent.

note: in some programming language like haskell you can overload by return types see Function overloading by return type? for more info

Community
  • 1
  • 1
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141