Methods in Programming
While writing a program, we may need to repeat some operations in different places. In this case, we can use methods instead of typing the same lines each time.
For example, if we need to print the elements of an array in several places, we can create a method and write the necessary codes inside this method and call this method from anywhere in the main program.
Methods are activated where they are called and the lines written in them are executed.
Using methods gives the programmer significant advantages. Some of these are those:
-
It reduces the complexity of the program and increases its comprehensibility.
-
It saves retyping the same operations.
-
When a change needs to be made in repeated operations, we just need to change the inside of the method . Otherwise we have to change every line where we write the same code.
-
Makes it easy to find fault.
Before a method can be used, it must first be defined. An example is shown below. When we examine the example, you will see that a method named arrayPrint is defined.
The Print Array method is defined in the main program, in the Form1 class. Therefore, it will be accessible from the same class.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent( );
}
public void arrayPrint ( )
{
// actions to be taken
}
private void button1_Click(object sender, EventArgs e)
{
arrayPrint( );
}
}
The public statement is the method's access specifier. When defining the method, the access limit must first be specified.
Access Modifiers
When defining methods, properties, and similar structures, it is specified where they can be accessed. This is called encapsulation . Encapsulation allows accessing to certain items to be restricted for security purposes. The access specifiers used in the C# programming language are:
-
public : An access restriction is not applied to items defined as public. They are accessible from anywhere.
-
private : The strictest access specifier. Private elements can only be accessed within the class in which they are defined.
-
protected : Elements defined as protected can be accessed within the class it is in or from other classes that derive from this class.
-
internal : These items can only be accessed within the same program.
-
protected internal : Considered protected and having an internal access specifier.
We can call publicly defined methods from anywhere in the program. In the example, the arrayPrint method is called from button1_Click.
The void statement specifies that the method does not return a value. If the method we defined will return a value, the type of that value is written here instead of void.
The method is then given a name. This name will be used when calling the method.
Parenthesis is opened and closed after the method name. If the method will not take any external value, these parentheses are left blank. What to do if the method is going to take value from outside, in the continuation of the article...
Let's examine the methods in 4 steps according to whether they take external values and return values:
1. Non-External Value and Non-Returning Methods
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void clearForm( )
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
label1.Text = "";
label2.Text = "Information Cleared.";
comboBox1.Items.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
// some operations
clearForm( );
// some operations
}
}
In the above example, a method named clearForm is defined. Parentheses after the method name are empty because this method does not take any external value. It is also stated that it will not return a value with the void statement.
Methods that do not return a value cannot be used by setting them to a variable or property. For example, we use the Clear Information above method a = clearForm( ); We cannot use it as
A method that does not return a value is used only by writing its name, and by writing it with empty parentheses since it does not take any external value.
Let's explain this work by comparing the ready-made methods we use. Since methods such as ToString, Parse, Pow return a value, they are definitely used by being equal to something.
int a = Math.Pow( a, b );
label1.Text = result.ToString( );
The Clear method, which clears the elements of the ComboBox object, is called without being synchronized, as it does not return a value.
comboBox1.Items.Clear( );
Let's not think that we can't print something from inside the form objects because our clear information method does not return a value. In the example, the contents of the text box and labels are already changed.
2. Methods That Take Value From The Outside, Don't Return Value
In order to define a method in such a way that it can take value from outside, the type and names of the data to be included in the parenthesis in the definition line are specified. (The information the method receives is called a parameter)
public void RepeatAndWrite( int a )
{
for ( int i = 1; i <= a; i ++ )
label1.Text += i + textBox1.Text + "\n";
}
private void button1_Click(object sender, EventArgs e)
{
int NumberofRepeats = int.Parse(textBox2.Text);
RepeatAndWrite(NumberofRepeats);
}
The RepeatAndWrite method described above which will take an external value of type int.
The value it receives from outside will be taken into a variable named a and operations can be performed inside the method with this variable.
An error will occur if no parameter is specified or if more than one parameter is specified when calling the RepeatAndWrite method. A value of type int must be sent when calling the method, because the method is defined to receive an external value.
RepeatAndWrite(NumberofRepeats); In the line, the number of repeats variable is passed to the method as a parameter. The method will use this value in itself by taking it into the variable a.
A method can take more than one external value. In this case, the values sent when calling the method must be in the same order as when defining it. Let's explain with an example.
public void calc( int a, int b )
{
long result = a;
for ( int i = 1; i <= b; i ++ )
result = result * a
label1.Text = result.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
int x= int.Parse(textBox1.Text);
int y= int.Parse(textBox2.Text);
calc( x, y);
}
In the above example, the calc method takes two values of type int from the outside. The method is called by sending the values of the x and y variables from button1_Click.
The value of the x variable will be set to the variable a in the method, and the value of the y variable will be set to the variable b in the method and the method will work in this way.
It is mandatory to send 2 parameters to thecalc method, no more or less than 2 can be sent.
3. Non-External Value-Returning Methods
When defining a method that returns a value, the type of the value to be returned is written instead of the void statement. String, int, long, double etc.
When calling such a method, it must be set to a variable or property.
Parentheses will be left blank because they do not take any external value.
Method that returns a random number between 0 and 1000:
public int numberGenerate( )
{
Random random = new Random();
int number = random.Next(0,1000);
return number;
}
private void button1_Click(object sender, EventArgs e)
{
int a = numberGenerate( );
}
Above, the return type of the numberGenerate method is specified as int. Therefore, when calling this method, we should use it by assigning it to a compatible variable or property.
The return statement is extremely important. This command specifies which value the method will return. In the example, the value of the number variable in the method is returned as the result.
* After the method is defined, a red warning will be given until the return statement is written. When you type the return command, the error will go away.
return 55; If we did, this method would always return 55 no matter what it does.
We need to make sure that we are returning the correct information with the return command.
4. Methods that Take Value from the Outside and Return a Value
After what has been explained above, we can proceed directly to the example.
public string combine( string text, int a )
{
string result = "";
for ( int i = 1; i <= a; i ++ )
result += text + "\n";
return result;
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = combine( "Victory", 5 );
}
The repeat method takes a string and an int parameter. While being summoned, "Victory" and 5 information were sent in the same order. The result is calculated in the method and returned as a string.
While calling the method, it is called by assigning the Text property of label1. In this way, the result will be written directly into the label.
c# method definition, c# methods, using methods, what is a method, what is method used for, how to create a method in c# language, how to create a method that returns a value, how to define a method that takes a value from outside
EXERCISES
There are no examples related to this subject.
Read 993 times.