Introduction
A static class is created using the "Static" keyword in C#. Static classes cannot be instantiated or inherited, but they can be accessed by static members only (static method, static variable, static constructor and etc..) but cannot be accessible to the non-static data members. We cannot create an instance for a static class.
Flow Chart
Figure. Static Class Members
How to create a static class?
- Create a class Name Details (or) keep it as you wish.
- Create the Static class by using the “Static” Keyword.
- Write the following code to create a static class
Syntax
static class Details
{
}
How to Create a Static Method?
- Create the Static Method by using the “Static” Keyword.
- Create a Method Name Main (or) keep it as you wish.
- Write the following code to create a static Method
Syntax
public static class Details
{
// Static Method
public static void Main(string FirstName, string LastName, string City)
{
}
}
How to create a Static Variable?
- Static variables can be initialized outside the member function or class definition.
- Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance.
Syntax
public static class Details
{
// Static Variables
public static int Sno;
public static string FirstName;
public static string LastName;
public static string City;
}
How to Create a Static Constructor?
- The static constructor does not take access modifiers.
- A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically
Syntax
public static class Details
{
public static int Sno;
public static string FirstName;
public static string LastName;
public static string City;
// Static Constructor
static Details()
{
Sno = 1;
FirstName = "ABCD";
LastName = "A";
City = "World";
}
}
Example
- Create a class Name Common (or) keep as it as you wish.
- Write the following code to create a static Class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Static_Class
{
public static class Common
{
public static string Details(string FirstName, string LastName, string City)
{
return FirstName + " " + LastName;
}
}
}
To create a method, call the Static class named Common and implement the method.
Write the following code.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Static_Class
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow: Window
{
public MainWindow()
{
InitializeComponent();
}
public void GetDetails()
{
var details = Common.Details(txtFirstName.Text, txtLastName.Text, txtCity.Text);
MessageBox.Show("Welcome to " + details, "Details");
}
private void BtnInsert_Click(object sender, RoutedEventArgs e)
{
GetDetails();
}
}
}
Output
Summary
In this article, I discussed the Static class and Static Members in C#. I have written this article focusing on beginners and students.