Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is a non-static class in C#?
A non-static class in C# is a regular class that can be instantiated using the new keyword. Unlike static classes, non-static classes allow you to create multiple objects (instances) and can contain both instance members and static members.
Non-static classes are the default type of class in C# and form the foundation of object-oriented programming, enabling encapsulation, inheritance, and polymorphism.
Syntax
Following is the syntax for declaring a non-static class −
public class ClassName {
// instance fields
// static fields
// instance methods
// static methods
// constructors
}
Following is the syntax for creating an instance of a non-static class −
ClassName objectName = new ClassName();
Key Features of Non-Static Classes
-
Can be instantiated using the
newkeyword to create multiple objects. -
Support inheritance − can inherit from other classes and be inherited by other classes.
-
Can contain both instance and static members − fields, methods, properties, and events.
-
Have constructors and destructors for object initialization and cleanup.
-
Instance members are accessed through object references, while static members are accessed through the class name.
Using Non-Static Classes with Instance Members
Example
using System;
public class Calculate {
public int number;
public Calculate(int num) {
number = num;
}
public int Square() {
return number * number;
}
public int Cube() {
return number * number * number;
}
}
public class Program {
public static void Main() {
Calculate calc1 = new Calculate(5);
Calculate calc2 = new Calculate(3);
Console.WriteLine("calc1 square: " + calc1.Square());
Console.WriteLine("calc1 cube: " + calc1.Cube());
Console.WriteLine("calc2 square: " + calc2.Square());
Console.WriteLine("calc2 cube: " + calc2.Cube());
}
}
The output of the above code is −
calc1 square: 25 calc1 cube: 125 calc2 square: 9 calc2 cube: 27
Using Non-Static Classes with Static Members
Example
using System;
public class MathUtils {
public int instanceValue;
public static int staticValue = 100;
public MathUtils(int value) {
instanceValue = value;
}
public int GetInstanceValue() {
return instanceValue;
}
public static int Add(int a, int b) {
return a + b;
}
public static int GetStaticValue() {
return staticValue;
}
}
public class Program {
public static void Main() {
MathUtils obj1 = new MathUtils(10);
MathUtils obj2 = new MathUtils(20);
Console.WriteLine("obj1 instance value: " + obj1.GetInstanceValue());
Console.WriteLine("obj2 instance value: " + obj2.GetInstanceValue());
Console.WriteLine("Static Add method: " + MathUtils.Add(15, 25));
Console.WriteLine("Static value: " + MathUtils.GetStaticValue());
}
}
The output of the above code is −
obj1 instance value: 10 obj2 instance value: 20 Static Add method: 40 Static value: 100
Comparison
| Feature | Non-Static Class | Static Class |
|---|---|---|
| Instantiation | Can create multiple instances using new
|
Cannot be instantiated |
| Inheritance | Supports inheritance and can be inherited | Sealed, cannot inherit or be inherited |
| Members | Can have both instance and static members | Can only have static members |
| Constructors | Can have instance and static constructors | Can only have static constructors |
| Access | Instance members via object, static via class name | All members via class name only |
Conclusion
Non-static classes are the standard classes in C# that can be instantiated to create objects. They support both instance and static members, inheritance, and constructors, making them essential for object-oriented programming and creating flexible, reusable code structures.
