0% found this document useful (0 votes)
9 views13 pages

Methods in C#

This document provides an overview of methods in C#, detailing their definition, usage, and key concepts such as method parameters, arguments, and return values. It also covers advanced topics like method overloading and the use of 'out' and 'ref' keywords for passing arguments by reference. The differences between 'ref' and 'out' are highlighted, along with guidance on when to use each keyword.

Uploaded by

ranaibrahim453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Methods in C#

This document provides an overview of methods in C#, detailing their definition, usage, and key concepts such as method parameters, arguments, and return values. It also covers advanced topics like method overloading and the use of 'out' and 'ref' keywords for passing arguments by reference. The differences between 'ref' and 'out' are highlighted, along with guidance on when to use each keyword.

Uploaded by

ranaibrahim453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Methods in C#

Overview and some advanced topics


Part 1
Methods
• A method is a code block that contains a set of statements.
• Any action that an object can perform are defined in methods.
• A program causes the statements to be executed by calling the
method and specifying any required method arguments.
• In C#, every executed instruction or action is performed in a
method.
• Unlike C++, Python and many other languages where methods
can exist independently, Methods in C# declared in either a class
or struct and cannot exist independently.
Methods
• The name of the method, and method parameters together
are called the signature of the method, return type is not
considered a part of method signature.
class SampleClass
{
public int SampleMethod(string parameter)
{
// code here
}
}
Methods

• Method Access
Calling a method on an object is like accessing a field. After the object
name, add a period, the name of the method, and parentheses.
Arguments are listed within the parentheses, and are separated by
commas. The methods of the Motorcycle class can therefore be called
as in the following example:
Obj.method_name(“arguments”);
Methods
Method Parameters vs. Arguments
• Parameters act as variables inside the method which will store data that is
needed as input by an method.
E.g. double multiply(double n1, double n2); //here
(double n1, double n2)are parameters that will store the input value
given to the method.
• When calling code calls the method, it provides concrete values for each
parameter these values are called arguments. The value given as arguments
must be compatible with the parameter (data) type.
E.g. Form1.multiply(20.4,50.5); // here 20.4 and 50.5
are arguments, the values that are provided as input
to the multiply method.
Methods
• Methods can return a value to the calling code. The return keyword followed by a
value that matches the return type will return that value to the method.
• The return keyword also stops the execution of the method. If the return type is void,
a return statement without a value will stop the execution of the method otherwise
the method will stop when it reaches the last code line.
• Methods that are not declared void are required to use the return keyword to return a
value. if return statement is missing in such a method the compiler will generate error.
class SimpleMath
{
public double Square_Number(double number)
{
return number * number;
} }
Methods
• To use a value returned from a method, the calling method can use
the method call itself anywhere a value of the same type would be
sufficient. You can also assign the return value to a variable.

int result = obj.Square_Number(3.14159);


result = obj.SquareANumber(result);
// The result is 9.8696.
MessageBox.Show(result);
Methods
Method Overloading
With method overloading, multiple methods can have the same name
with different parameters: E.g.
int MyMethod(int x)
float MyMethod(float x)
double MyMethod(double x, double y)

Instead of defining two methods that should do the same thing, it is


better to overload one.
Method Overloading has nothing to do with return type.
Understanding out and ref Keywords
• In C#, both out and ref keywords are used to pass arguments by
reference to methods.

• This allows a method to modify the value of a variable passed to it.

• They are used when a method needs to returns multiple values


out
• The out is a keyword in C# which is used for the passing the arguments to methods
as a reference type.

• It is generally used when a method returns multiple values.

• It is not necessary to initialize parameters before it pass to out.

• It is necessary to initialize the value of the out parameter variable before returning
to the calling method.

• When out keyword is used the data only passed unidirectional (out of method).
ref
• The ref is a keyword in C# which is used for the passing the arguments by a reference.

• If any changes are made in this argument in the method will reflect in that variable
when the control return to the calling method.

• It is necessary the parameters should initialize before it pass to ref.

• It is not necessary to initialize the value of a parameter before returning to the calling
method.

• When ref keyword is used the data may pass in bi-directional.


Differences Between ref and out
Keywords

Feature ref out


Initialization Must be initialized before passing Does not need to be initialized
Must be assigned a value before
Assignment Modification is optional
method exits
Used when the value is passed and Used when the method is expected
Purpose
modified to return a new value
Returning multiple values from a
Example Usage Updating an existing value
method
When to Use ref and out?
• Use ref when the method needs to read and modify an existing
variable.

• Use out when a method needs to return multiple values and the
variables are uninitialized before calling the method.

• These keywords are useful in scenarios like retrieving status codes,


returning multiple values from a function, or modifying method
parameters efficiently

You might also like