KEMBAR78
Loops | PDF | Parameter (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
58 views2 pages

Loops

The document discusses different types of methods in C#, including methods that return values versus those that do not, and how to call methods. It also covers method parameters, allowing methods to accept input, and method overloading, which allows multiple methods to have the same name but different parameters. Key points include: - Methods are defined using the method name followed by parentheses. They can return values using return or not return anything. - Parameters pass input to methods and are declared like variables, starting with lowercase letters by convention. - Method overloading lets a method name be used for multiple versions that differ in parameters, to group related functionality.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

Loops

The document discusses different types of methods in C#, including methods that return values versus those that do not, and how to call methods. It also covers method parameters, allowing methods to accept input, and method overloading, which allows multiple methods to have the same name but different parameters. Key points include: - Methods are defined using the method name followed by parentheses. They can return values using return or not return anything. - Parameters pass input to methods and are declared like variables, starting with lowercase letters by convention. - Method overloading lets a method name be used for multiple versions that differ in parameters, to group related functionality.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Here are two examples—one method that doesn’t return anything, and one that does:

// This method doesn't return any information.


void MyMethodNoReturnedData()
{
// Code goes here.
}
// This method returns an integer.
int MyMethodReturnsData()
{
// As an example, return the number 10.
return 10;
}
Notice that the method name is always followed by parentheses. This allows the compilerto recognize that it’s a method. In
this example, the methods don’t specify their accessibility. This is just a common C# convention. You’re free to add an
accessibility keyword (such as public or private) as shown
here:
private void MyMethodNoReturnedData()
{
// Code goes here.
}
The accessibility determines how different classes in your code can interact. Private methods are hidden from view and are
available only locally, whereas public methods can be called by all the other classes in your application. To really understand
what this means, you’ll need to read the next chapter, which discusses accessibility in more detail.Invoking your methods is
straightforward—you simply type the name of the method, followedby parentheses. If your method returns data, you have
the option of using the data it returns or just ignoring it:
// This call is allowed.
MyMethodNoReturnedData();
// This call is allowed.
MyMethodReturnsData();
// This call is allowed.
int myNumber;
myNumber = MyMethodReturnsData();
// This call isn't allowed.
// MyMethodNoReturnedData() does not return any information.
myNumber = MyMethodNoReturnedData();
Parameters
Methods can also accept information through parameters. Parameters are declared in a similar way to variables. By
convention, parameter names always begin with a lowercase letter in any language. Here’s how you might create a function
that accepts two parameters and returns their
sum:
private int AddNumbers(int number1, int number2)
{
return number1 + number2;
}
When calling a method, you specify any required parameters in parentheses or use an empty set of parentheses if no
parameters are required:
// Call a method with no parameters.
MyMethodNoReturnedData();
// Call a method that requires two integer parameters.
MyMethodNoReturnedData2(10, 20);
// Call a method with two integer parameters and an integer return value.
int returnValue = AddNumbers(10, 10);
CHAPTER 2 n THE C# LANGUAGE 47
Method Overloading
C# supports method overloading, which allows you to create more than one method with the same name, but with a different
set of parameters. When you call the method, the CLR automatically chooses the correct version by examining the
parameters you supply. This technique allows you to collect different versions of several methods together. For example,
you might allow a database search that returns an array of Product objects representing records in the database. Rather than
create three methods with different names depending on the criteria, such as GetAllProducts(), GetProductsInCategory(), and
GetActiveProducts(), you could create three versions of the GetProducts() method. Each method would have the same name
but a different signature, meaning it would require different parameters. This example provides two overloaded versions for
the GetProductPrice()
method:
private decimal GetProductPrice(int ID)
{
// Code here.
}
private decimal GetProductPrice(string name)
{
// Code here.
}
// And so on...
Now you can look up product prices based on the unique product ID or the full product name, depending on whether you
supply an integer or string argument:
decimal price;
// Get price by product ID (the first version).
price = GetProductPrice(1001);
// Get price by product name (the second version).
price = GetProductPrice("DVD Player");
You cannot overload a method with versions that have the same signature—that is, the same number of parameters and
parameter data types—because the CLR will not be able to distinguish them from each other. When you call an overloaded
method, the version that matches the parameter list you supply is used. If no version matches, an error occurs.
There is special type of methods called delegate methods which i will discuss later

You might also like