Sunday, December 23, 2018

C# Enum (Enumeration)

C# Enumeration

An enumeration is used in any programming language to define a constant set of values. For example, the days of the week can be defined as an enumeration and used anywhere in the program. In C#, the enumeration is defined with the help of the keyword 'enum'.
Let's see an example of how we can use the 'enum' keyword.

In our example, we will define an enumeration called days, which will be used to store the days of the week. For each example, we will modify just the main function in our Program.cs file.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Program 
 {
  enum Days{Sun,Mon,tue,Wed,thu,Fri,Sat};
  
  static void Main(string[] args) 
  {
   Console.Write(Days.Sun);
   
   Console.ReadKey();
  }
 }
}

Code Explanation:-

  1. The 'enum' data type is specified to declare an enumeration. The name of the enumeration is Days. All the days of the week are specified as values of the enumeration.
  2. Finally the console.write function is used to display one of the values of the enumeration.
If the above code is entered properly and the program is executed successfully, the following output will be displayed.


Output:












Sunday, December 2, 2018

C# Data Types with Example

1) Integer

An Integer data types are used to work with numbers. In this case, the numbers are whole numbers like 10, 20 or 30. In C#, the datatype is denoted by the Int32 keyword. Below is an example of how this datatype can be used. In our example, we will define an Int32 variable called num. We will then assign an Integer value to the variable and then display it accordingly


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Program
 { 
  static void Main(string[] args) 
  {
   Int32 num=30;
   Console.Write(num);  
   
   Console.ReadKey();
  }
 }
}
Code Explanation:- 

The Int32 data type is specified to declare an Integer variable called num. The variable is then assigned a value of 30. Finally the console.write function is used to display the number to the console. If the above code is entered properly and the program is executed successfully, following output will be displayed.



     From the output, you can clearly see that the Integer variable called num was displayed in the console

2) Double

A double data type is used to work with decimals. In this case, the numbers are whole numbers like 10.11, 20.22 or 30.33. In C#, the datatype is denoted by the keyword "Double". Below is an example of this datatype.In our example, we will define a double variable called num. We will then assign a Double value to the variable and then display it accordingly.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Program
 { 
  static void Main(string[] args) 
  {
   double num=30.33;
   Console.Write(num); 
   
   Console.ReadKey();
  }
 }
}

Code Explanation:-

  1. The double data type is specified to declare a double type variable called num. The variable is then assigned a value of 30.33.
  2. Finally the console.write function is used to display the number to the console.
If the above code is entered properly and the program is executed successfully, following output will be displayed.

Output:


From the output, you can clearly see that the double variable called num was displayed in the console

3) Boolean

A boolean data type is used to work with Boolean values of true and false. In C#, the datatype is denoted by the Boolean keyword. Below is an example of this datatype can be used.

In our example, we will define a Boolean variable called 'status.' We will then assign a boolean value to the variable and then display it accordingly.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Program
  { 
   static void Main(string[] args) 
   {
    Boolean status=true;
    Console.Write(status);
    
    Console.ReadKey();
   }
  }
}

Code Explanation:-

  1. The boolean data type is specified to declare a Boolean variable called 'status.' The variable is then assigned a value of true/false.
  2. Finally the console.write function is used to display the Boolean value to the console.
If the above code is entered properly and the program is executed successfully, the output will be displayed.

Output:

From the output, you can clearly see that the Boolean variable which equals true was displayed in the console

4) String

A String data type is used to work with String values. In C#, the datatype is denoted by the keyword 'String'. Below is an example of this datatype.In our example, we will define a String variable called 'message.' We will then assign a String value to the variable and then display it accordingly.







using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class program
 {
  static void Main(string[] args)
  {
   String message="Hello";
   Console.Write(message);
   
   Console.ReadKey();
  }
 }
}

Code Explanation:-

  1. The String data type is specified to declare a string variable called message. The variable is then assigned a value of "Hello".
  2. Finally, the console.write function is used to display the string value to the console.
If the above code is entered properly and the program is executed successfully, the output will be displayed.

Output:

From the output, you can clearly see that the String variable called message was displayed in the console

 

































 








C# Enum (Enumeration)

C# Enumeration An enumeration is used in any programming language to define a constant set of values. For example, the days of the week c...