Diggie
Diggie

Reputation: 142

Shallow copy in c#

I understand the definition of shallow copy

Shallow copying is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type --> a bit-by-bit copy of the field is performed; for a reference type --> the reference is copied but the referred object is not; therefore the original object and its clone refer to the same object.

but why static fields are not copied?

Upvotes: 5

Views: 340

Answers (3)

It'sNotALie.
It'sNotALie.

Reputation: 22794

Because static fields are not part of the object. You could access them with ClassName.StaticValue. In fact, you CAN'T access them with ClassInstanceName.StaticValue.

Upvotes: 10

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Because static members are class level members not instance level which mean that static members common for all instances of given type.

Upvotes: 2

Andrew Savinykh
Andrew Savinykh

Reputation: 26280

Static fields are shared by all the instances of a particular class.

Upvotes: 1

Related Questions