Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to call a method of a class in C#
To call a method of a class in C#, you need to create an instance of the class first, then use the dot notation to access its methods. The general syntax is objectName.MethodName(parameters).
Syntax
Following is the syntax for calling an instance method −
ClassName objectName = new ClassName(); returnType result = objectName.MethodName(parameters);
For static methods, you call them directly on the class without creating an instance −
returnType result = ClassName.StaticMethodName(parameters);
Using Instance Methods
Instance methods require an object of the class to be called. Here's how you create an object and call its methods −
using System;
class ApplicationOne {
public int displayMax(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
public void showInfo(string message) {
Console.WriteLine("Info: " + message);
}
static void Main(string[] args) {
/* local variable definition */
int a = 700;
int b = 400;
int ret;
ApplicationOne one = new ApplicationOne();
// calling the displayMax method
ret = one.displayMax(a, b);
Console.WriteLine("Max value is : {0}", ret);
// calling the showInfo method
one.showInfo("Method call successful");
}
}
The output of the above code is −
Max value is : 700 Info: Method call successful
Using Static Methods
Static methods belong to the class itself rather than any specific instance. They can be called directly using the class name −
using System;
class Calculator {
public static int Add(int x, int y) {
return x + y;
}
public static double Multiply(double x, double y) {
return x * y;
}
static void Main(string[] args) {
// calling static methods directly on class
int sum = Calculator.Add(15, 25);
double product = Calculator.Multiply(4.5, 3.2);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Product: " + product);
}
}
The output of the above code is −
Sum: 40 Product: 14.4
Method Call Types Comparison
| Instance Method | Static Method |
|---|---|
| Requires object creation | Called directly on class |
| objectName.MethodName() | ClassName.MethodName() |
| Can access instance variables | Cannot access instance variables |
| Memory allocated per object | Single copy in memory |
Calling Methods with Different Parameters
Methods can accept various types of parameters and return different data types −
using System;
class Student {
public string name;
public int age;
public void SetDetails(string studentName, int studentAge) {
name = studentName;
age = studentAge;
}
public string GetDetails() {
return "Name: " + name + ", Age: " + age;
}
public bool IsAdult() {
return age >= 18;
}
static void Main(string[] args) {
Student student = new Student();
// calling method with parameters
student.SetDetails("Alice", 20);
// calling method that returns string
string details = student.GetDetails();
Console.WriteLine(details);
// calling method that returns boolean
bool adult = student.IsAdult();
Console.WriteLine("Is Adult: " + adult);
}
}
The output of the above code is −
Name: Alice, Age: 20 Is Adult: True
Conclusion
Calling methods in C# involves creating an object instance for instance methods or using the class name directly for static methods. Use dot notation to access methods, and ensure you pass the correct parameters and handle return values appropriately.
