Syntax to declare variables in C sharp

  1. When the variable are declare under the class, by default every variable having a default value which will be zero for integer type ,False for Boolean types, Null for string and object type. Where as if a variable is declared under any block of code it is must to initialized those variable at the time of declaration.
  2. The default scope for the member of class is private.
  3. A constant variable cannot be modified once after its declaration so it is must to assign value at the time of its declaration.
  4. A readonly variable can not be modified once after its initialization so at the time of declaration it is not mandatory to assign a value, it can be initialized after the declaration also.
  5. Decimal value are by default treated as double so we need to use as f-suffix to treated as a float and m-suffix to the value to treat as decimal.

Example To get the data Type Of a variable

using System; namespace GetDataTypeApplication { class TypesDemo { static void Main(string[] args) { int i = 10; Console.WriteLine(i); Console.WriteLine(i.GetType()); float f = 3.14f; Console.WriteLine(f); Console.WriteLine(f.GetType()); double d = 58.27; Console.WriteLine(d); Console.WriteLine(d.GetType()); decimal de = 43.273m; Console.WriteLine(de); Console.WriteLine(de.GetType()); } } }
Note:- "GetType" is a predefined method that returns the type of given variable or Instance.

No comments:

Post a Comment