05 Methods and Structures
05 Methods and Structures
namespace ConsoleApp
{
class DemoMethod
{
static void Main(string[] args)
{
DemoMethod obj = new DemoMethod (); //this creates new instance of object
//this invoke the void method
obj.printRectArea(5, 3);
//this method computes and prints the area of a rectangle of given width and height values
public void printRectArea(int width, int height)
{
int area = width * height;
Console.WriteLine("The area of rectangle is " + area);
}
//this method computes the area of a rectangle of given width and height values then return
the result
public int getRectArea(int width, int height) {
int area = width * height;
return area;
}
}
}
In Code Listing 1, the new instance of object named obj is created, which is used to call the methods. Additionally, the method
printRectArea is called with two (2) arguments, which are 5 and 3:
obj.printRectArea(5, 3);
This method performs its operation and does not return a value. If a method returns a value, the return data can be assigned
to a variable. In Code Listing 1, the method getRectArea is called with two (2) arguments and performs its operation then
returns a value:
int result = obj.getRectArea(5, 3);
When calling the method printRectArea or getRectArea, the width and height values are passed as arguments to the
method, which are assigned to its parameters. The method performs its operation from the values receivedthroughparameters.
Method Overloading
C# supports method overloading. Method overloading means that methods of the same name can be declared in the same
class as long as they have different sets of parameters which are determined by the number, types, and order ofthe parameters.
It is used to create several methods with the same name that perform the same or similar task, but on differenttypesordifferent
numbers of arguments. For example, the following statements define three (3) overloaded versions of getArea method,each
with a different set of parameters and different operations.
Example 3. Three (3) overloaded versions of getArea method:
//this method takes one (1) parameter and computes the area of a square
public int getArea(int side) {
int area = side * side;
return area;
}
//this method of int return type takes two (2) parameters and computes the area of a rectangle
public int getArea(int width, int height)
int area = width * height;
return area;
}
//this method of double return type takes two (2) parameters and computes the area of a rectangle
public double getArea(double width, double height)
double area = width * height;
return area;
}
Output:
9 / 15 / 17.0625
When an overload method is called in C#, the Common Language Runtime (CLR) compiler selects the appropriate method
depending on the number and type of defined arguments. The method overloading provides flexibility in supplyingarguments.
Structures
C# provides a value type data type called structure. Structures are used to make a single variable that holds related data of
various data types. The struct keyword is used for creating a structure. Structures are used to represent a record.The following
shows the general form how to define structures:
Syntax: Example of a struct declaration:
access_modifier struct StructName { public struct Book {
//structure members( fields, methods, and constructors) public string title;
} public string author;
public long book_id;
}
Structures can also contain constructors and methods. They are not allowed to initialize the defined struct fields. Forexample,
the field public string title = "Object-Oriented Programming"; will cause an error to the program.
To initialize struct fields, you need to create a new instance of struct object in the class using the default constructor. The
following example creates an instance of a Book struct object and initializes and accesses its individual members.
Book book1 = new Book();
//initializing struct members
book1.title = "Object-Oriented Programming";
book1.author = "John Doe";
book1.book_id = 20190001;
//accessing struct members
Console.WriteLine("Book title: " + book1.title);
Console.WriteLine("Book author: " + book1.author);
Console.WriteLine("Book ID: " + book1.book_id);
Output:
Book title: Object-Oriented Programming
Book author: John Doe
Book ID: 20190001
Structure is different from class. Structures are value types because its variables contain values, while classes are reference
types that store the memory address of where the actual value is stored. Unlike structures, instance variables of a class can be
initialized. Structures can implement an interface, but they cannot inherit from another structure.
namespace ConsoleApp
{
public struct Book {
public string title;
public string author;
public int book_id;
class DemoStruct
{
static void Main(string[] args)
{
Book book1 = new Book();
//initializing struct members
book1.title = "Object-Oriented Programming";
book1.author = "John Doe";
book1.book_id = 20190001;
//accessing struct members
Console.WriteLine("Book title: " + book1.title);
Console.WriteLine("Book author: " + book1.author);
Console.WriteLine("Book ID: " + book1.book_id);
//new instance of Book struct object and using its methods
Book book2 = new Book();
book2.setValues("C# Programming", "Jane Doe", 20190002);
book2.displayValues();
}
}
}
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program (5th Ed.). USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C# (4th Ed.). USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.