Eg: for parsing: Void main() { Console.WriteLine("Enter your age: "); int age = int.parse(Console.ReadLine()); Console.WriteLine("Your age is " +age); Console.ReadLine(); } Output: Enter your age 300 Your age is 300
Drawback: Using parsing we can convert from only string to any other data type that means parse method will accept input as only strings.
3. Converting: In converting we will use a predefined class called convert.
Convert class will have various data type conversion methods like below:
Using converting we can convert from any data type to any other data type. Syntax: <To datatype> <To variable> = Convert.<Conversion method>(<from variable>);
Eg: for converting: Void main() { Console.WriteLine("Enter first number"); int a = Convert.Toint32(Console.ReadLine()); Console.WriteLine("Enter second number"); int b = convert.Toint32(Console.ReadLine()); int c = a + b; Console.WriteLine("C value is " +c); Console.ReadLine(); } Output: Enter first number 10 Enter second number 5 C value is 15
Boxing & Unboxing:
1. Boxing: Boxing is a process of converting from value type to reference type.
Eg: From int to object:
2. Un-Boxing: Unboxing is a process of converting from reference type to value type.
Eg: From object to int. Synatx: <To datatype> <Tovariable> = <(To datatype)> <from variable>
Ex: For boxing & unboxing: Void main() { int i = 25; object obj = (object) i; // Boxing Console.WriteLine("Obj value is " +obj); int j = (int) obj; // Unboxing Console.WriteLine("j value is " +j); Consloe.ReadLine(); } Output: obj value is 25 j value is 25
1. Class & Object: Class is a collection of members, it can contain variables, properties, methods, constructors, destructors, events & indexers & So on..
Syntax: To declare a class <Access modifier> class <class name> { <Variables>; <Properties>; <Methods>; <Constructors>; <Destructors>; <Events>; <Indexers>; }