Thursday 13 March 2014

C# Structure

Why C# structures cannot have explicit parameterless constructor?

Answer: structures are value types. value types in C# contains default values. 
example: integer which by default value is numerical zero.

Remember : Structures can contain parametrized constructor.

more on C# structure

  • To define a structure we will use C# keyword called "struct"
  • structure is a value type. (its a protocol)
  • when we instantiated structure , it will be allocated in stack memory.(because all struct is a value type and all value types are will be allocated a stack memory)
  • structure cannot contain destructor.(only class can have destructor) WHY? 

Answer: struct being value type, are not subject to garbage collection. for more click here
  • we cannot use abstract and sealed keyword while defining a structure. C# Struct does not support inheritance.
  • we cannot use protected and protected internal access modifier for structures or structure members because it will not support inheritance.
  • To create an object for structure we don't required to use new operator.(When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.)


C#.NET Interface

C Sharp - Interface

  • By default Interface is Internal
  • All interface members are implicitly public and abstract so the members of the interface never specify access modifier.
  • Interfaces do not have an implementation of members(protocol). so interfaces cannot have data fields and also interfaces do not have constructors.
  • It is illegal to allocate Interface types.
  • Interfaces has read/write properties
  • Interfaces can contain event and indexer definitions.