For Intermediates: C# Methods.(part-1)
Definition:
A method is a code block containing a series of statements.
Methods must be declared within a class or a structure. It is a good
programming practice that methods do only one specific task. Methods bring
modularity to programs.
We need to be able to break large programs into
smaller chunks that are easy to handle.
C# lets you divide your class code into chunks known as methods. Properly designed and implemented
methods can greatly simplify the job of writing complex programs in
c#.Methods change the state of the
objects created. They are the dynamic part of the objects; data is the static
part.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Sample
{
public int a; // Nonstatic
public static int b; // Static
public void InstanceMethod() // Nonstatic
{
Console.WriteLine(“instance method”);
}
public static void ClassMethod() // Static
{
Console.WriteLine(“ class method”);
}
}
static void Main(string[] args)
{
Sample ex = new Sample();
ex.a = 1;
Sample.b=2;
}
}
// in the above
code indicate it is comments and the code follows // will not be executed.
In the above programming example to invoke a
nonstatic — instance — method, you need an instance of the class. To invoke a
static — class — method, you call via the class name, not an instance. The
following code snippet assigns a value to the object data member a and the
class, or static, member b. note in the above program static keyword is used to
indicate static variable and static methods.
Sample
ex = new Sample(); // Create an instance of class Example.
ex.a
= 1; // Initialize instance member through instance.
Sample.b
= 2; // Initialize class member through class
The following snippet defines and
accesses InstanceMethod() and ClassMethod() in almost the same way:
Sample ex=new Sample();
ex.InstanceMethod();
Sample.classMethod();
The expression ex.InstanceMethod() passes
control to the code contained within the method. C# follows an almost identical
process for Sample.ClassMethod().
Output:
instance method
class method
After a method completes
execution, it returns control to the point where it was called. That is,
control moves to the next statement after the call.
Sample :
We start with a simple
example.
using System;
public class Base
{
public void viewInfo()
{
Console.WriteLine("Base class");
}
}
public class SimpleMethod
{
static void Main()
{
Base bs = new
Base();
bs.viewInfo();
}
}
Each method must be
defined inside a class or a struct. It must have a name. In our case the name
is ViewInfo. The keywords that precede the name of the method are access
specifier and the return type. Parentheses follow the name of the method. They
may contain parameters of the method. Our method does not take any parameters.
static void Main()
{
...
}
Passing an argument to a method
The values you input to a method
are method arguments, or parameters.Most methods require some
type of arguments if they’re going to do somethingYou pass arguments to a
method by listing them in the parentheses that follow the method name. Consider
this small addition to the earlier Sample class:
public class Sample
{
public static void Output(string
someString)
{
Console.WriteLine(“Output() was
passed the argument: “ + someString);
}
}
I could invoke this method from
within the same class, like this:
Output(“Hai”);
I would then see this
not-too-exciting output:
Output() was passed the argument:
Hai
----will continue