partialconstants.md - brainchildservices/curriculum GitHub Wiki

  1. What is a partial class?

    • C# provides a concept to write source code in separate files and compile it as a single unit. This feature is called partial types.
  2. Which keyword is used to implement partial class?

    • The partial keyword is used to create partial types.
  3. What is the use of partial methods in C#?

    • A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not.
  4. What is a Constant variable?

    • A variable whose value can not be changed during the execution of the program is called a constant variable.
  5. Define compile time constants.

    • The compile time constants are declared by using the const keyword, in which the value can not be changed during the execution of the program.
  6. Define run time constants.

    • A runtime constant is a value that is computed only while the program is running.
  7. When to use const?

    • The const is used when its value is absolutely constant.
  8. Define properties of const.

    • const can be declared at the class level as well as inside the method. const can not be declared using static keyword because they are by default static. constants are absolute constants in which their value cannot be changed or assigned at the run time. constant variables are compile time variables.
  9. Define properties of readonly.

    • Readonly can be declared only at the class level, not inside the method. Readonly can not be declared using static keywords because they are by default static. Readonly constant's value can be set through the reference variable. Readonly constant variables are a runtime time constant variable.
  10. When to use Readonly?

  • We can use Readonly when its value is not an absolute constant, which means it can be changed frequently.
  1. Differentiate between readonly and const variable.

  • const fields need to be initialized with declaration only, while readonly fields can be initialized at declaration or in the constructor. const variables can be declared in methods, while readonly fields cannot be declared in methods. const fields cannot be used with static modifier, while readonly fields can be used with static modifier. const field is a compile-time constant, the readonly field can be used for run time constants.