Methods Video Tutorial
Click here for video tutorial on Different Types of Method Parameters
Is the following code legal?
using System;
namespace Demo
{
class Program
{
public static void Main()
{
}
public void Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
public int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
}
}
No, The above code does not compile. You cannot overload a method based on the return type. To overload a method in C# either the number or type of parameters should be different. In general the return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.
What is the difference between method parameters and method arguments. Give an example?
In the example below FirstNumber and SecondNumber are method parameters where as FN and LN are method arguments. The method definition specifies the names and types of any parameters that are required. When calling code calls the method, it provides concrete values called arguments for each parameter. The arguments must be compatible with the parameter type but the argument name (if any) used in the calling code does not have to be the same as the parameter named defined in the method.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
int FN = 10;
int SN = 20;
//FN and LN are method arguments
int Total = Sum(FN, SN);
Console.WriteLine(Total);
}
//FirstNumber and SecondNumber are method parameters
public static int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
return Result;
}
}
}
Explain the difference between passing parameters by value and passing parameters by reference with an example?
We can pass parameters to a method by value or by reference. By default all value types are passed by value where as all reference types are passed by reference. By default, when a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method.An example is shown below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
int I = 10;
int K = Function(I);
Console.WriteLine("I = " + I);
Console.WriteLine("K = " + K);
}
public static int Function(int Number)
{
int ChangedValue = Number + 1;
return ChangedValue;
}
}
}
By default, reference types are passed by reference. When an object of a reference type is passed to a method, the reference points to the original object, not a copy of the object. Changes made through this reference will therefore be reflected in the calling method. Reference types are created by using the class keyword as shown in the example below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
ReferenceTypeExample Object = new ReferenceTypeExample();
Object.Number = 20;
Console.WriteLine("Original Object Value = " + Object.Number);
Function(Object);
Console.WriteLine("Object Value after passed to the method= " + Object.Number);
}
public static void Function(ReferenceTypeExample ReferenceTypeObject)
{
ReferenceTypeObject.Number = ReferenceTypeObject.Number + 5;
}
}
class ReferenceTypeExample
{
public int Number;
}
}
Can you pass value types by reference to a method?
Yes, we can pass value types by by reference to a method. An example is shown below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
int I = 10;
Console.WriteLine("Value of I before passing to the method = " + I);
Function(ref I);
Console.WriteLine("Value of I after passing to the method by reference= " + I);
}
public static void Function(ref int Number)
{
Number = Number + 5;
}
}
}
If a method's return type is void, can you use a return keyword in the method?
Yes, Even though a method's return type is void, you can use the return keyword to stop the execution of the method as shown in the example below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
SayHi();
}
public static void SayHi()
{
Console.WriteLine("Hi");
return;
Console.WriteLine("This statement will never be executed");
}
}
}
What is the difference between static class and class with static methods? In which case I should use either of them?
ReplyDeleteQ:What is the difference between static class and class with static methods? In which case I should use either of them?
ReplyDeleteA: If your class has only static members you never need an instance of that class so you should make the class itself static but if your class has instance members (non static) then you have to make your class an instance class to access its instance members via instances of your class.
who invented dotnet
ReplyDeleteThere's one more reason for the code in your first example to fail the compilation: the second (attempted) overload method declares return type "int", but lacks the "return Result;" statement.
ReplyDeleteIf the purpose of the example is to demonstrate the fact that methods cannot be overloaded solely by altering the return type, I suggest fixing this
;-)
Why would you use ref to pass a reference type?
ReplyDeleteDo we use 'ref' key word while passing reference types?. I don't think we need to do that. If i'm not correct Please explain with an example.
ReplyDeleteAbout Static class : We can not instantiate, inherit the static class.
ReplyDeleteAbout the class with full of static methods: We can instantiate this class. But logically we are not going to do anything with these instances. So practically this scenario doesn't exist. However, it might be useful in inheriting.. But practically I haven't come across such situation anywhere.
When it some to static class , we use it to extend a particular object. For example we have accounts object. We need to add a method HasBalance(). We can do it by extension method.
However, it is very common to have classes with both static and non-static methods.