An instance method is one that belongs to an instance of the class, so you first need to create an object of the class before calling the method.
using System;
class Car
{
// Instance method
public void StartEngine()
{
Console.WriteLine("Engine started!");
}
public void Drive(string destination)
{
Console.WriteLine($"Driving to {destination}");
}
}
class Program
{
static void Main()
{
// Creating an instance of the Car class
Car myCar = new Car();
// Calling instance methods using the instance
myCar.StartEngine(); // Output: Engine started!
myCar.Drive("New York"); // Output: Driving to New York
}
}
Explanation:
2. Calling a Static Method
A static method belongs to the class itself, not an instance. You can call a static method directly using the class name without creating an object of the class.
using System;
class MathOperations
{
// Static method
public static int Add(int a, int b)
{
return a + b;
}
public static void DisplayMessage()
{
Console.WriteLine("This is a static method!");
}
}
class Program
{
static void Main()
{
// Calling static methods using the class name
int result = MathOperations.Add(5, 3); // Output: 8
Console.WriteLine($"The sum is: {result}");
MathOperations.DisplayMessage(); // Output: This is a static method!
}
}
Explanation:
3 . Calling a Method from Another Class Instance
You can also call a method from one class inside another class. Here’s an example where one class calls a method from another class.
using System;
class Car
{
public void StartEngine()
{
Console.WriteLine("Car engine started!");
}
}
class Driver
{
public void DriveCar(Car car)
{
// Calling method from Car class
car.StartEngine(); // Output: Car engine started!
Console.WriteLine("Driver is driving the car.");
}
}
class Program
{
static void Main()
{
Car myCar = new Car();
Driver driver = new Driver();
driver.DriveCar(myCar); // The Driver calls the Car's method
}
}
Explanation:
The Driver class has a method DriveCar that accepts an object of the Car class and calls the StartEngine method of the Car class using car.StartEngine().
An instance method is one that belongs to an instance of the class, so you first need to create an object of the class before calling the method.
using System; class Car { // Instance method public void StartEngine() { Console.WriteLine("Engine started!"); } public void Drive(string destination) { Console.WriteLine($"Driving to {destination}"); } } class Program { static void Main() { // Creating an instance of the Car class Car myCar = new Car(); // Calling instance methods using the instance myCar.StartEngine(); // Output: Engine started! myCar.Drive("New York"); // Output: Driving to New York } }
Explanation:
2. Calling a Static Method
A static method belongs to the class itself, not an instance. You can call a static method directly using the class name without creating an object of the class.
using System; class MathOperations { // Static method public static int Add(int a, int b) { return a + b; } public static void DisplayMessage() { Console.WriteLine("This is a static method!"); } } class Program { static void Main() { // Calling static methods using the class name int result = MathOperations.Add(5, 3); // Output: 8 Console.WriteLine($"The sum is: {result}"); MathOperations.DisplayMessage(); // Output: This is a static method! } }
Explanation:
3 . Calling a Method from Another Class Instance
You can also call a method from one class inside another class. Here’s an example where one class calls a method from another class.
using System; class Car { public void StartEngine() { Console.WriteLine("Car engine started!"); } } class Driver { public void DriveCar(Car car) { // Calling method from Car class car.StartEngine(); // Output: Car engine started! Console.WriteLine("Driver is driving the car."); } } class Program { static void Main() { Car myCar = new Car(); Driver driver = new Driver(); driver.DriveCar(myCar); // The Driver calls the Car's method } }
Explanation:
The Driver class has a method DriveCar that accepts an object of the Car class and calls the StartEngine method of the Car class using car.StartEngine().
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved