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 cover the basics of classes in C#. A class is a blueprint for creating objects, and it defines the data and behavior of an object. Classes can contain fields, properties, methods, constructors, and more.
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 this example, the Person
class has a field Name
, a property Age
, a constructor that takes name
and age
as parameters, and a method Greet
that displays a greeting message.
In the Main
method, create an instance of the Person
class:
static void Main(string[] args) { // Instantiate the Person class Person person = new Person("John Doe", 30); // Call the method person.Greet(); Console.ReadLine(); }
When you run the Console Application, you should see the following output:
Hello, my name is John Doe and I am 30 years old.
Let's briefly explain the different types of class members used in this example:
Field: A field is a variable that is declared directly in a class. Fields are used to store data for an object. In our example, Name
is a field.
Property: A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. In our example, Age
is a property.
Constructor: A constructor is a special method that is called when an object is created. It is used to initialize the object's fields and properties with default or provided values. In our example, Person(string name, int age)
is a constructor.
Method: A method is a block of code that performs a specific action or calculation. Methods can be called on objects to execute their functionality. In our example, Greet()
is a method.
In this tutorial, we've covered the basics of classes in C#, including fields, properties, constructors, and methods. Classes are fundamental building blocks in object-oriented programming and are essential for creating objects and organizing your code in C#.
C# class definition example:
public class MyClass { // Class members go here }
Creating and using classes in C#:
public class MyClass { public void DisplayMessage() { Console.WriteLine("Hello from MyClass!"); } } // Usage MyClass myObject = new MyClass(); myObject.DisplayMessage();
Class constructor in C#:
public class MyClass { // Constructor public MyClass() { Console.WriteLine("Constructor called!"); } } // Usage MyClass myObject = new MyClass(); // Constructor called!
C# class properties and methods:
public class MyClass { // Property public string MyProperty { get; set; } // Method public void DisplayProperty() { Console.WriteLine($"Property value: {MyProperty}"); } } // Usage MyClass myObject = new MyClass(); myObject.MyProperty = "Hello, World!"; myObject.DisplayProperty(); // Output: Property value: Hello, World!
Encapsulation in C# classes:
public class MyClass { // Private field private string myField; // Public property with encapsulation public string MyProperty { get { return myField; } set { myField = value; } } }
Inheritance in C# classes:
public class MyBaseClass { public void BaseMethod() { Console.WriteLine("Base method called!"); } } public class MyDerivedClass : MyBaseClass { // Additional members in the derived class } // Usage MyDerivedClass derivedObject = new MyDerivedClass(); derivedObject.BaseMethod(); // Output: Base method called!
Abstract classes in C#:
public abstract class MyAbstractClass { // Abstract method without implementation public abstract void AbstractMethod(); } public class MyDerivedClass : MyAbstractClass { // Implementing the abstract method public override void AbstractMethod() { Console.WriteLine("Abstract method implemented in the derived class."); } } // Usage MyAbstractClass abstractObject = new MyDerivedClass(); abstractObject.AbstractMethod(); // Output: Abstract method implemented in the derived class.
Static classes in C#:
public static class MyStaticClass { public static void StaticMethod() { Console.WriteLine("Static method called!"); } } // Usage MyStaticClass.StaticMethod(); // Output: Static method called!