A class is a blueprint; an object is a runtime instance created with new. If you know Java classes, C# feels familiar—everything lives in types—but C# adds properties, value types, and reference semantics on the CLR heap.
Defining and using a class
class User {
public string Name = "";
public void Greet() {
Console.WriteLine($"Hi, {Name}");
}
}
var u = new User { Name = "Ada" };
u.Greet();
Object initializer syntax sets fields or properties at construction time. All objects are reference types unless declared as structs.
Reference semantics
User a = new User(); User b = a; — both references point to the same object; mutating through b affects a—same model as Java, unlike value-copy structs in C++ stack objects.
Important interview questions and answers
- Q: Class vs object?
A: Class is the type definition; object is a heap instance with its own field values. - Q: new keyword?
A: Allocates memory on the managed heap and runs the constructor; GC reclaims when unreachable.
Self-check
- What operator creates an object?
- How does C# OOP differ from JavaScript prototypes?
Tip: Class instances are reference types on the managed heap—assigning b = a copies the reference, not the object, like Java.
Interview prep
- What does new do?
Allocates a reference-type instance on the managed heap and runs the constructor.
- Every class inherits from?
object—all types getToString,Equals, andGetHashCode.