Monday, December 29, 2014

C# - Classes and Structs

Classes and structs encapsulates set of data and behaviors into a logical unit. A class is a reference type while struct is of value type. When a struct is assigned to a new variable it is copied. Struct is like a light-weight class. (Read)


Note: the new operator is used to create and invoke objects. The new operator doesn't mean it is a reference type. It says the type has a constructor. All value and reference types has constructors at least the default one. (SO).

int i = new int(); equals to
int A = new int(); or int A = default(int);

Encapsulation defines how accessible the members of the classes are to the outside of the class. Class can have fields, constants, properties, methods, constructors, destructors, events, indexers, operators as well as nested types. (See Class members article)

When defining a struct if you're adding a parameterized constructor, you must initialize all fields in that (Source). Also it is not a good practice to use mutable structs. See examples of mutable structs. 

Inheritance : Classes support inheritance. A class can derive from another class (base class). Structs does not support inheritance because it is of value type. Classes can be declared as abstract meaning, one or more methods have no implementation. Classes and structs can inherit multiple interfaces. 
See : Why structs cannot be inherited, Why structs needs to be boxed

Generic Types : Classes and structs can be defined with one or more type parameters.

Static types : Classes can be declared as static while structs cannot. A static class can only contain static members and cannot be instantiated with new keyword. Both classes and structs can contain static members

Nested types : class and struct can be nested within another class or struct. (See Nested Types - MSDN)

Partial classes : You can also define a class in two files using partial

Object initializers : You can instantiate class or struct object without explicitly calling the constructor.  (MSDN)

Anonymous types : When it is not necessary to create named classes, you can use anonymous types. (See Anonymous Types in C# Programming guide)

var msg = new { Id = 108, Message = "Hello" };

Anonymous types are typically used in select clause of query expressions (LINQ). Anonymous type can contain  one or more public read-only fields. It cannot contain anything else (such as methods, event handlers etc.). 

Extension methods : You can extend a class without creating a derived class by creating separate types using extension methods.

Implicitly typed local variables : You can use these variables to instruct the compiler to determine correct type at runtime. (See implicitly typed local variables)


Resources
https://roslyn.codeplex.com/discussions/568824
http://programmers.stackexchange.com/questions/92339/when-do-you-use-a-struct-instead-of-a-class

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook