You are on page 1of 5

Input and print a float value in C#

Read any value, we use Console.ReadLine() method and if the desired value is not in the string format, we need to
convert it into the specific type.

1) float.Parse() method – Here, float is an alias of Single class and Parse() is its method – it converts


given string value to the float value.
Syntax:

float_value = float.Parse(Console.ReadLine());

2) Single.Parse() method – Single is a class and Parse() its method – it converts a string value to the


float value.
Syntax:

float_value = Single.Parse(Console.ReadLine());

3) Convert.ToSingle() method – Here, ToSingle() is a method of Convert class – it converts given


object into the float value.
Syntax:

float_value = Convert.ToSingle(Console.ReadLine());

typeof() Operator
typeof() is an operator in C#, it is used to get the type (system type) of with class name of a given type. By
using typeof() operator, we can get the name of the type, namespace name. It works with only compile-time known
types. typeof() operator does not work with the variables or instances.

get the type of a variable, you can use GetType() method

Properties to get the details about the type:

1. typeof(type).Name or this.GetType().Name – It returns the class name only.


2. typeof(type).FullName or this.GetType().FullName – It returns the class name along with the
namespace.
3. typeof(type).Namespace or this.GetType().Namespace – It returns the namespace only.

Note: If we do not use any property, by default typeof(type) or this.GetType() returns the FullName.

Syntax:

System.type typeof(type);
or
System.type this.GetType();
Eg. Print the Name, FullName, Namespace name of the compile-time known types.

using System;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("for char type...");
Console.WriteLine("default: " + typeof(char));
Console.WriteLine("Name: " + typeof(char).Name);
Console.WriteLine("FullName: " + typeof(char).FullName);
Console.WriteLine("Namespace: " + typeof(char).Namespace);
Console.WriteLine();
Console.WriteLine("for Int32 type...");
Console.WriteLine("default: " + typeof(Int32));
Console.WriteLine("Name: " + typeof(Int32).Name);
Console.WriteLine("FullName: " + typeof(Int32).FullName);
Console.WriteLine("Namespace: " + typeof(Int32).Namespace);
Console.WriteLine();
Console.WriteLine("for bool type...");
Console.WriteLine("default: " + typeof(bool));
Console.WriteLine("Name: " + typeof(bool).Name);
Console.WriteLine("FullName: " + typeof(bool).FullName);
Console.WriteLine("Namespace: " + typeof(bool).Namespace);
Console.WriteLine();
Console.ReadLine();
}
}
}

C# sizeof() Operator
sizeof() is an operator in C#, it is used to get the size in bytes of compile-time known types, it does not work with the
variables or instances.

Syntax:

int sizeof(type);
It accepts the type and returns an int value 
Eg.
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("size of {0} is {1} bytes", typeof(bool), sizeof(bool));
Console.WriteLine("size of {0} is {1} bytes", typeof(byte), sizeof(byte));
Console.WriteLine("size of {0} is {1} bytes", typeof(char), sizeof(char));
Console.WriteLine("size of {0} is {1} bytes", typeof(UInt32), sizeof(UInt32));
Console.WriteLine("size of {0} is {1} bytes", typeof(ulong), sizeof(ulong));
Console.WriteLine("size of {0} is {1} bytes", typeof(decimal), sizeof(decimal));
Console.ReadLine();
}
}
}

Exception handling in C#
An exception is a runtime error; that means an abnormal situation which is created at run time and the program
doesn’t execute successfully. Due to the exceptions, our program gets crash.

Common exceptions are occurred during program:

 Divide by Zero
 File not found
 Array Index out of bound
 Null reference exception
 Invalid cast exception
 Arithmetic exception
 Overflow exception
 Out of memory exception etc.

Eg.

using System;
class Program
{
static void Main()
{
int number1 = 0;
int number2 = 0;
int result = 0;
Console.WriteLine("Enter Number : ");
number1 = int.Parse(Console.ReadLine());
result = number1 / number2;
Console.WriteLine("Result : " + result);
}
}

The above program generates "Divide by Zero exception" and the program gets crashed. Because
variable number1 is divided by number2 and output stored in the result variable. Where, the value of number2 is 0,
thus, "divide by zero exception" occurred.

To handle such kind of exceptions, we use exception handling.

Exception handling in C# can be handled using three blocks

1. try
2. catch
3. finally
Syntax of exception handling

try
{
// code that can occur an exception
}
catch(Exception Type)
{
// code to handle the exception
// code to display the error message
}
finally
{
// code that should be executed
// whether exception is occurred or not
}

1) try block

In this block, we write the code that can produce an exception or runtime error. For example, in the previous
program, there was an exception (divide by zero).

2) catch block

This block is executed when the code throws an exception that is written in a try block, catch block catches
the exception and we can write the code to handle that exception or any message to display the exception
as per user preference.

Here, we used the exception classes, there are the following exception classes used in C# to catch the
exceptions.

1. Exception
2. DivideByZeroException
3. NullReferenceException
4. OutOfMemoryException
5. InvalidCastException
6. ArrayTypeMismatchException
7. IndexOutOfRangeException
8. AirthmeticException
9. OverFlowExecption

These classes are available in System namespace. In the above classes, the Exception class is a superclass and others
are sub-classes, The Exception class can catch any kind of exception thrown by the try block.

3) finally block

finally block is executed in all cases i.e. whether the program is generating an exception or not, before
leaving the program, compiler moves to the finally block. This block can be used to write the codes that are
compulsory for the execution of program like the closing of the file, releasing the memory, etc.
Eg.
using System;

class Program
{
static void Main()
{
int number1 = 0;
int number2 = 0;
int result = 0;

try
{
Console.WriteLine("Enter Number : ");
number1 = int.Parse(Console.ReadLine());

result = number1 / number2;


Console.WriteLine("Result : " + result);
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Program exited normally");
}
}
}

Output

You might also like