Monday 25 March 2013

Constructors in C Sharp


  • Constructors are the core concept of Object Oriented Language, So the compiler generates a default constructor with default field values at compile time, only if there is no custom constructor was created.
  • If the class or struct has a custom constructor , the compiler silently removes the default constructor.
  • When a class or struct is created , its constructor is called(Invoked when a class is instantiated and objects memory location is created).
  • Constructors can be overloaded and they don't return values so no return type is used including void.
  • we can call one constructor from another using 'this' keyword.
  • Car(int modelNumber) :  this() {  }
  • We can also use 'base' keyword if the class is inherited and wants the parent class constructor values to be loaded to child class.
  • Constructors should be public so that values will be assigned to current state(fields).
  • If private, we cannot instantiate the class(because the constructor will not be available or not accessible outside of that class.)
  • we can also create static constructor but only static fields can be assigned.
  • If struct , parameter constructors are not allowed.
  • It is legal to create custom constructor(struct type) with no parameters. As struct type is value type 'new' keyword to allocate memory object is optional.
  • Remember: Static constructors will be executed at random time but before the class is instantiated.

Sunday 24 March 2013

What is the difference between runtime and compile time?


Answer: Runtime and compile time are programming terms that refer to different stages of software program development. In order to create a program, a developer first writes source code, which defines how the program will function. Small programs may only contain a few hundred lines of source code, while large programs may contain hundreds of thousands of lines of source code. The source code must be compiled into machine code in order to become and executable program. This compilation process is referred to as compile time.
A compiled program can be opened and run by a user. When an application is running, it is called runtime.
The terms "runtime" and "compile time" are often used by programmers to refer to different types of errors. A compile time error is a problem such as a syntax error or missing file reference that prevents the program from successfully compiling. The compiler produces compile time errors and usually indicates what line of the source code is causing the problem.
If a program's source code has already been compiled into an executable program, it may still have bugs that occur while the program is running. Examples include features that don't work, unexpected program behavior, or program crashes. These types of problems are called runtime errors since they occur at runtime.

Source: PC.net