Your Position: Home - Packaging & Printing - What does => operator pointing from field or a method ...
This question already has answers here:
What does "=>" do in .Net C# when declaring a property? [duplicate](2 answers)
Closed
7 years ago
.I've seen an operator => used in the following example:
public int Calculate(int x) => DoSomething(x);
or
public void DoSoething() => SomeOtherMethod();
I have never seen this operator used like this before except in lamba expressions.
What does the following do? Where, when should this be used?
Methods are generally the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, it provides a better readability of code. So basically, a method is a collection of statements that perform some specific task and return the result to the caller. A method can also perform some specific task without returning anything.
Example :
// Method Name --> GetCircleArea() // Return Type ---> double static double GetCircleArea(double radius) { const float pi = 3.14F; double area = pi * radius * radius; return area; }
Method Declaration
Method declaration means the way to construct method including its naming.
Syntax :
<Access_Modifier> <return_type> <method_name>([<param_list>])
In C# a method declaration consists of the following components as follows :
Method Signature : Method Signature is defined by mainly two parameters(number of parameters, type of the parameters and order of the parameters), One of them is Method Name and second one is its Parameter list.
Method Naming : Name of a method or a function in any programming language whether in C++ or Java or C# holds great importance and is mainly used in order to call that method for its execution. For example, findSum, computeMax, setX and getX etc. There are certain pre-defined rules for naming methods which a user should follow :
These rules are not mandatory, but recommendable. Generally, a method has a unique name within the class in which it is defined but sometime a method might have the same name as other method names within the same class as method overloading is allowed in C#.
The Method Body : As discussed above the body of the method consists of statements of code which a user wants to perform. After the method has been declared, it is dependent on the user whether to define its implementation or not. Not writing any implementation, makes the method not to perform any task. However, when the user wants to perform certain tasks using method then it must write the statements for execution in the body of the method. The below syntax describes the basic structure of the method body :
Syntax :
<return_type> <method_name>(<parameter_list>) { // Implementation of the method code goes here..... }
Method Calling
Method Invocation or Method Calling is done when the user wants to execute the method. The method needs to be called for using its functionality. A method returns to the code that invoked it when:
Example : In the code below, a method named Sum() is called.
using
System;
namespace
ConsoleApplication1 {
class
Geeks {
static
int
Sum(
int
x,
int
y)
{
int
a = x;
int
b = y;
int
result = a + b;
return
result;
}
static
void
Main(
string
[] args)
{
int
a = 12;
int
b = 23;
int
c = Sum(a, b);
Console.WriteLine(
"The Value of the sum is "
+ c);
}
}
}
Output :
The Value of the sum is 35
Method Parameters
There might be certain situations the user want to execute a method but sometimes that method requires some value inputs in order to execute and complete its tasks. These input values are known as Parameters in a computer language terms. Now, these parameters can be either int, long or float or double or char. However, it depends upon the user requirements. The methods in C# can be classified into different categories based on return type as well as input parameters.
using
System;
namespace
ConsoleApplication2 {
class
Geeks {
static
void
PrintSentence()
{
Console.WriteLine(
"No parameters and return type void"
);
}
static
void
Main(
string
[] args)
{
PrintSentence();
}
}
}
No parameters and return type void
using
System;
namespace
ConsoleApplication3 {
class
Geeks {
static
int
sum()
{
int
a = 78, b = 70, add;
add = a + b;
return
add;
}
static
void
Main(
string
[] args)
{
int
getresult = sum();
Console.WriteLine(getresult);
}
}
}
148
using
System;
namespace
ConsoleApplication3 {
class
Geeks {
static
void
perimeter(
int
p)
{
Console.WriteLine(
"Perimeter of the Square is "
+ 4 * p);
}
static
void
Main(
string
[] args)
{
int
p = 5;
perimeter(p);
}
}
}
Perimeter of the Square is 20
using
System;
namespace
ConsoleApplication4 {
class
Geeks {
static
int
factorial(
int
n)
{
int
f = 1;
for
(
int
i = 1; i<= n; i++)
{
f = f * i;
}
return
f;
}
static
void
Main(
string
[] args)
{
int
p = 4;
Console.WriteLine(
"Factorial is : "
+ factorial(p));
}
}
}
Factorial is : 24
Advantages of using the Methods :
There are many advantages of using methods. Some of them are listed below:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
227
0
0
Comments
All Comments (0)