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
Fig: 1
How to create a static class
- Create a class Name Details (or) keep as it as your wish.
- Create the Static class by using “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 “Static” Keyword.
- Create a Method Name Main (or) keep as it as your 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 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
Static Constructor
- 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 your wish.
- Write the following code to create a static Class.
Coding
- 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
Coding
- 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();
- }
- }
- }
No comments:
Post a Comment