INTEGRATIVE PROGRAMMING [PREFI]
Methods
Definition: Group of statements that accomplish a specific task.
Syntax:
access_modifier return_type MethodName(parameter_list) { //statements }Components:
Access Modifier: Determines visibility (private/public). Default is private.
Return Type: Specifies if the method returns a value (e.g.,
int,void).Method Name: Identifier for the method, case sensitive.
Parameter List: Defines required data types and order of parameters.
Method Body: Contains the logic enclosed in
{}.
Example Method Signatures
public void printRectArea(int width, int height)- prints area.public int getRectArea(int width, int height)- returns area.
Method Invocation
Syntax:
MethodName(arguments).Arguments correspond to parameters.
Method Overloading
Definition: Multiple methods with the same name but different parameters in the same class.
Provides flexibility with different argument types and numbers.
Example overloads:
public int getArea(int side)- area of a square.public int getArea(int width, int height)- area of a rectangle.public double getArea(double width, double height)- area with double parameters.
Structures
Definition: Value type to group related data of different types.
Declaration:
access_modifier struct StructName { //members }.Example:
public struct Book {
public string title;
public string author;
public long book_id;
}
Structures cannot initialize fields directly. Must use a constructor to instantiate.
Comparison with Classes:
Structures are value types; classes are reference types.
Structures do not allow inheritance but can implement interfaces.
Instance variables of classes can be initialized directly.
Conclusion
Understanding methods and structures in C# allows for organized coding and data management, supporting object-oriented programming principles effectively.