C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll explore how to call class members in C#. Class members include fields, properties, methods, and events. We'll create a simple class and demonstrate how to instantiate it and call its members.
Create a new C# Console Application project in Visual Studio.
Create a sample class called Person
with the following members: a field, a property, a method, and a constructor:
public class Person { // Field public string Name; // Property public int Age { get; set; } // Constructor public Person(string name, int age) { Name = name; Age = age; } // Method public void Greet() { Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old."); } }
In the Main
method, create an instance of the Person
class and call its members:
static void Main(string[] args) { // Instantiate the Person class Person person = new Person("John Doe", 30); // Call the field and property Console.WriteLine($"Name: {person.Name}"); Console.WriteLine($"Age: {person.Age}"); // Call the method person.Greet(); Console.ReadLine(); }
When you run the Console Application, you should see the following output:
Name: John Doe Age: 30 Hello, my name is John Doe and I am 30 years old.
In this tutorial, we've demonstrated how to call class members in C#. We created a simple class with a field, a property, a method, and a constructor, and then instantiated the class and called its members. This basic understanding of calling class members is essential for working with classes and objects in C#.
C# calling class methods example:
public class MyClass { public void MyMethod() { Console.WriteLine("This is a method in MyClass."); } } // Usage MyClass myObject = new MyClass(); myObject.MyMethod();
Accessing class properties and methods in C#:
public class MyClass { public string MyProperty { get; set; } public void MyMethod() { Console.WriteLine($"Property value: {MyProperty}"); } } // Usage MyClass myObject = new MyClass(); myObject.MyProperty = "Hello, World!"; myObject.MyMethod();
Calling static methods in C# classes:
public class MyClass { public static void MyStaticMethod() { Console.WriteLine("This is a static method in MyClass."); } } // Usage MyClass.MyStaticMethod();
C# invoke class constructor:
public class MyClass { public MyClass() { Console.WriteLine("Constructor of MyClass invoked."); } } // Usage MyClass myObject = new MyClass();
Accessing class fields in C#:
public class MyClass { private int myField; public void SetField(int value) { myField = value; } public void DisplayField() { Console.WriteLine($"Field value: {myField}"); } } // Usage MyClass myObject = new MyClass(); myObject.SetField(42); myObject.DisplayField();
Calling class methods from another class in C#:
public class MyClassA { public void MethodA() { Console.WriteLine("MethodA in MyClassA."); } } public class MyClassB { public void CallMethodA() { MyClassA objA = new MyClassA(); objA.MethodA(); } } // Usage MyClassB objB = new MyClassB(); objB.CallMethodA();