C# Unit I 2024 25 Revised
C# Unit I 2024 25 Revised
UNIT-I Introduction to C#
*Introduction- The .NET Framework is a software development platform that was introduced
by Microsoft on 13th February 2002
The base class library has a rich collection of libraries features and functions that help to
implement many programming languages in the .NET Framework, such as C #, F #, Visual C
++, and more. Furthermore, BCL divides into two parts:
1. User defined class library
Assemblies –it is a File which is automatically generated by compiler upon
successful compilation.
It is the collection of small parts of deployment an application's part.
It contains either the .DLL (Dynamic Link Library) or .exe (Executable) file.
2. Predefined class library
Namespace - It is the collection of predefined classes and methods that present
in .NET.
In other languages such as, C we used header files, in java we used package
similarly we used "using system" in .NET, where using is a keyword and system is
a namespace.
FCL (Framework Class Library)-
The Framework Class Library or FCL is a superset of BCL which provides the
system functionality in the .NET Framework as it has various classes, data types,
interfaces, etc. to perform multiple functions and build different types of
applications such as desktop applications, web applications, mobile applications,
etc.
3) CLS (Common Language Specification)-
CLS is a subset of CTS. It defines a set of rules and restrictions that every language must
follow which runs under the .NET framework. The languages which follow these set of rules
are said to be CLS Compliant.
The Microsoft Intermediate Language (MSIL), also known as the Common Intermediate
Language (CIL) is a set of instructions that are platform independent and are generated by the
language-specific compiler from the source code.
The MSIL is platform independent and consequently, it can be executed on any of the Common
Language Infrastructure supported environments such as the Windows .NET runtime.
The execution process that includes the creation of the MSIL and the conversion of the MSIL
into machine code by the JIT compiler is given as follows:
5) Garbage Collector
Garbage Collector is responsible for automatic memory management of objects.
Also, the responsibility of the garbage collector is removing the object from memory that has
no use.
* .NET Core
.NET Core, developed by Microsoft, is managed under the .NET Foundation, a non-
profit open source organization.
.NET Core is written in C# and C++ and licensed under MIT (Massachusetts Institute
of Technology) license. The first version, .NET Core 1.0, was released in 2016 with
limited functionality.
.NET Core includes a runtime, a set of libraries, and a development environment that
supports multiple programming languages such as C#, Visual Basic, and F#.
It can be used to build a wide range of applications, including web applications, desktop
applications, Mobile applications, Cloud applications, Gaming applications, Internet of
Things and micro services etc.
Key characteristics of .NET Core include open source, cross-platform, modern, flexible,
lightweight, fast, friendly, shareable, and built for future software development.
1) Free and Open Source.
The .NET Core platform is free and open source.
.NET Core source code project is available on GitHub.
Any developer can get involved in .NET Core development. Thousands of active
developers participating in .NET Core development are improving features, adding new
features, and fixing bugs and issues.
.NET Core is free and licensed under MIT and Apache licenses.
2) Cross-Platform.
.NET Core supports and runs on Windows, macOS, and Linux operating systems.
.NET Core is consistent across architecture, including x64, x86, and ARM.
The same assemblies and libraries can be imported and used on multiple platforms.
The assemblies and libraries are built using one of the .NET languages, C#, [Link], or
F#.
3) Sharable.
.NET Core uses one consistent API model written in .NET Standard that is common to
all .NET applications.
The same API or library can be used with multiple platforms in multiple languages.
4) Modern.
Unlike some older frameworks, .NET Core is designed to solve today's modern needs,
including being mobile friendly, build once run everywhere, scalable, and high
performance.
.NET Core is designed to build applications that target all kinds of devices, including
IoTs and gaming consoles.
.NET Core supports modern language constructs with the help of C# version 8, like
object-oriented and modular programming, generics, collections, lambdas, Language
Integrated Query (LINQ), and asynchronous programming, which makes developers
productive.
5) Fast.
.NET Core 3.0 is fast. Compared to the .NET Framework.
.NET Core is much quicker than other server-side frameworks such as Java Servlet and
[Link].
According to a report published by TechEmpowers, .NET Core is much faster than any other framework.
TechEmpower benchmark compares Web application frameworks for tasks such as database access for a single
query, multiple queries, fortunes, data updates, plaintext, and JSON serialization.
6) Lightweight.
.NET Core is lightweight.
.NET Core can be included in your app or installed on a server side-by-side user,
machine-wide, or. In addition, the .NET Core can be deployed in Docker containers.
7) .NET Core is Friendly.
.NET Core is compatible with .NET Framework, Xamarin, and Mono, via .NET
Standard.
.NET Core also supports working with various popular Web frameworks and libraries
such as React, Angular, and JavaScript.
TypeScript is one of the key components of the .NET Core and Visual Studio ecosystem.
*C# Basics-
Introduction-
C# is a Pure Object Oriented Programming Language under .NET Framework developed
by Microsoft.
C# is designed and developed by Anders Hejlsberg & his team members at Microsoft.
C# Variables-
Definition - A Variable is an identifier that denotes a storage location used to store data value.
OR
Variables are containers used to store data values.
A variable may take different values at different time during the execution of program.
Note-The function Convert.ToInt32()/ [Link] converts the data entered by the user to int
data type, because [Link]() accepts the data in string format.
C# Data Types-
Data type specify the size and type of values that can be stored.
Value Types:
In C#, Value Types are stored on the Stack and having Fixed Length.
When a value of variable is assigned to another variable, the value is actually copied. It means
that two identical copies of the value are available in memory.
Reference Types:
In C#, Reference Types are stored on the Heap and having variable (Not Fixed) Length.
When a value of reference variable is assigned to another reference variable, the reference
(address) is actually copied. It means that both variables are refers to same memory location.
*Nullable types-
In C#, the compiler does not allow you to assign a null value to a variable. So, C# 2.0 provides a
special feature to assign a null value to a variable that is known as the Nullable type.
The concept of Nullable type is useful when we deal with databases that contain elements that may
not be assigned a value.
C# provides two different ways to create Nullable types.
1. By creating [Link] instance,
2. By using ? operator
1. By creating [Link] instance
Syntax:
Nullable<data_type> variable_name = null;
Example
internal class Program
{
static void Main(string[] args)
{
Nullable<int> a= 10;
[Link]("Value in a="+a);
a = null;
[Link]("Value in a=" + a);
[Link]();
}
}
2. By using ? operator
Syntax:
datatype? variable_name = null;
Example
static void Main(string[] args)
{
int? a = 10;
[Link]("Value in a="+a);
a = null;
[Link]("Value in a=" + a);
[Link]();
}
Note- A Nullable Type is incompatible with a General Data Type. This means we cannot
operate between a nullable type and a general datatype.
For example
int? x = 4;
int y = 3;
int z = x * y;
[Link]([Link]());
[Link]();
Here x is nullable while y is not nullable. So The above Program will generate the following
compile time Error:
Error 1 Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you
missing a cast?)
null-coalescing Operator returns the left side operand if the operand is not null else if left side
operand is NULL then it returns the right side operand.
Example-
static void Main(string[] args)
{
int? left = null;//NULL
int right = 20;
}
Output-
Result=20
Example:2-
static void Main(string[] args)
{
int? left = 10;//NOT NULL
int right = 20;
}
Output-
Result=10
The null conditional operator (?.), often referred to as the "Elvis operator", allows you to
perform member access or method calls on an object only if that object is not null.
If the object is null, the operation returns null instead of throwing a null reference exception.
This operator is a game-changer for developers, as it significantly reduces the amount of code
needed to safely access members of potentially null objects.
Example-
class Employee
{
private string empname;
[Link](emp?.GetName());
}
}
A boxing conversion makes a copy of the value. So, changing the value of one variable will not
impact others.
Example: Boxing
int i = 10;
object ob = i; //performs boxing
In the above example, the integer variable that is "i" is assigned to the object "ob". Thus the
object data type is a reference type and base class of all the other classes in C# ultimately, an
integer can be assigned to an object type. And this process of converting from the integer to the
object data type is called boxing.
Unboxing is the reverse of boxing. It is the process of converting a reference type to value
type.
Unboxing extract the value from the reference type and assign it to a value type.
Unboxing is explicit. It means we have to cast explicitly.
Example: Unboxing
int i = 10;
object ob = i; //performs boxing
object ob = 10;
int i = (int)ob; //performs unboxing
Boxing Unboxing
It convert value type into an object type. It convert an object type into value type.
Here, the value stored on the stack copied to Here, the object stored on the heap memory
the object stored on the heap memory. copied to the value stored on the stack .
C# Keywords-
Keywords are predefined sets of reserved words that have special meaning in a program.
The meaning of keywords cannot be changed, neither can they be directly used as identifiers in a
program.
C# has a total of 79 keywords. All these keywords are in lowercase. Here is a complete list of all
C# keywords.
abstract as base bool
break byte case catch
char checked class const
continue decimal default delegate
do double else enum
event explicit extern false
finally fixed float for
foreach goto if implicit
in in (generic modifier) int interface
internal is lock long
namespace new null object
operator out out (generic modifier) override
params private protected public
readonly ref return sbyte
sealed short sizeof stackalloc
static string struct switch
this throw true try
typeof uint ulong unchecked
unsafe ushort using using static
void volatile while
Note-
Although keywords are reserved words, they can be used as identifiers if @ is added as prefix.
For example,
int @void;
The above statement will create a variable @void of type int.
*Type Inference-
Type inference refers to the automatic detection of the data type of an expression in a formal
language.
C# is a strongly typed language, and its default type declaration is explicit. This means we have to
specify a type for a new variable or the compiler will throw an error. With version 3 of C#, a new
keyword, var, was introduced that allows developers to store any type of value in an implicit way.
This means that the compiler decides during compile time, when the first assignment happens,
what the type will be for that variable. This can be easily integrated with LINQ.
[Link](“DataType of nm=”+[Link]());
[Link](“DataType of a=”+[Link]());
[Link](“DataType of name=”+[Link]());
[Link]();
}
}
[Link]("Prints on ");
[Link]("Same line");
}
}
}
When we run the program, the output will be
Prints on
New line
Prints on Same line
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
int firstNumber = 5, secondNumber = 10, result;
result = firstNumber + secondNumber;
[Link]("{0} + {1} = {2}", firstNumber, secondNumber, result);
}
}
}
When we run the program, the output will be
5 + 10 = 15
Here, {0} is replaced by firstNumber, {1} is replaced by secondNumber and {2} is replaced
by result. This approach of printing output is more readable and less error prone than
using + operator.
C# Input
In C#, the simplest method to get input from the user is by using the ReadLine() method of
the Console class.
However, Read() and ReadKey() are also available for getting input from the user.
They are also included in Console class.
Read(): The Read() method reads the next character from the standard input stream. It
returns the ascii value of the character.
ReadKey(): The ReadKey() method obtains the next key pressed by user.
This method is usually used to hold the screen until user press a key.
*C# Operators-
Operators in C# are symbols that are used to perform operations on operands.
For example, consider the expression 2 + 3 = 5, here 2 and 3 are operands and + and = are
called operators. So, the Operators in C# are used to manipulate the variables and values in a
program.
Types of Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Conditional Operator
6. Special Operator
In C#, the Operators can also be categorized based on the Number of Operands:
1. Unary Operator: The Operator that requires one operand (variable or value) to perform
the operation is called Unary Operator.(Ex. i++,-9)
2. Binary Operator: Then Operator that requires two operands (variables or values) to
perform the operation is called Binary Operator. (Ex. a+b)
3. Ternary Operator: The Operator that requires three operands (variables or values) to
perform the operation is called Ternary Operator. Ternary Operator is also called
Conditional Operator.( Ex. a>b?a:b)
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations such as addition,
subtraction, multiplication, division, etc.
C# Arithmetic Operators
2. Relational Operators
Relational operators are used to check the relationship between two operands. If the
relationship is true the result will be true, otherwise it will result in false.
Relational operators are used in decision making and loops.
C# Relational Operators
C# Relational Operators
3. Logical Operators
Logical operators are used to determine the logic between variables or values:
Operator Name Description Example
&& Logical and Returns true if both statements are true x < 5 && x < 10
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)
4. Assignment Operators
There are following assignment operators supported by C# −
Operator Description Example
= Simple assignment operator, Assigns values from right side operands to C=A+B
left side operand assigns value
of A + B into
C
+= Add AND assignment operator, It adds right operand to the left operand C += A is
and assign the result to left operand equivalent to
C=C+A
-= Subtract AND assignment operator, It subtracts right operand from the left C -= A is
operand and assign the result to left operand equivalent to
C=C-A
/= Divide AND assignment operator, It divides left operand with the right C /= A is
operand and assign the result to left operand equivalent to
C=C/A
class TernaryOperator
{
public static void Main(string[] args)
{
int number = 10;
string result;
result = (number % 2 == 0)? "Even Number" : "Odd Number";
[Link](number + “is” +result);
}
}
10 is Even Number
[Link] Operator-
Special operators are operators which are used to perform specific task.
Multiplicative *, /, %
Additive +, -
Category Operators
Equality ==, !=
Bitwise XOR ^
Bitwise OR |
Logical OR ||
Ternary ?:
Assignment =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
The assignment operators have the lowest precedence while the postfix increment and
decrement operators have the highest precedence.
*Type Conversion-
Generally, smaller types like int (having less memory size) are automatically converted to
larger types like double (having larger memory size) and conversions from derived classes to
base classes.
Example-
static void Main(string[] args)
{
int num = 10;
double db;
db = num; //Implicit Type Conversion.
[Link](db);
[Link]();
}
Explicit type conversion – In explicit type conversion, we explicitly convert one type to
another.
Generally, larger types like double (having large memory size) are converted to smaller types
like int (having small memory size).
These conversions are done explicitly by users using the pre-defined functions. Explicit
conversions require a cast operator.
Example-
static void Main(string[] args)
{
int num = 10;
double db;
db = num;
int tr = (int)db;//Explicit Type Conversion
[Link](tr);
[Link]();
}
}
}
}
Output:47
It is even number
2) if-else Statement
The C# if-else statement also tests the condition. It executes the if block if condition is true
otherwise else block is executed.
Syntax: if(condition) {
//code if condition is true
}
else {
//code if condition is false
}
using System;
public class IfExample
{ public static void Main(string[] args)
{ int num = 11;
if (num % 2 == 0)
{
[Link]("It is even number");
}
else
{
[Link]("It is odd number");
}
}
Output-
It is odd number
{
// code to be executed
}
}
Ex.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 5, y = 20;
if (x > y)
{
if (x >= 10)
{
[Link]("x value greater than or equal to 10");
}
else
{
[Link]("x value less than 10");
}
}
else
{
if (y <= 20)
{
[Link]("y value less than or equal to 20");
}
else
{
[Link]("y value greater than 20");
}
}
[Link]("Press Enter Key to Exit..");
[Link]();
}
}
}
{
// code to be executed if condition2 is true
}
else if(condition n)
{
// code to be executed if condition3 is true
}
...
else
{
// code to be executed if all the conditions are false
}
class Program {
public static void Main(String[] args)
{ int i = 20;
if (i == 10)
[Link]("i is 10");
else if (i == 15)
[Link]("i is 15");
else if (i == 20)
[Link]("i is 20");
else
[Link]("i is not present");
}
}
Output- i is 20
5) Switch Case-
Switch statement is an alternative to long if-else-if ladders.
The expression is checked for different cases and the one match is executed.
break statement is used to move out of the switch. If the break is not used, the control will flow
to all cases below it until break is found or switch comes to an end.
There is default case (optional) at the end of switch, if none of the case matches then default
case is executed.
Syntax-
switch (expression)
{
case value1: // statement sequence
break;
case value2: // statement sequence
break;
.
.
.
case value N : // statement sequence
break;
default: // default statement sequence
namespace Conditional
{
class SwitchCase
{
public static void Main(string[] args)
{
char ch;
[Link]("Enter an alphabet");
ch = [Link]([Link]());
switch([Link](ch))
{
case 'a':
[Link]("Vowel");
break;
case 'e':
[Link]("Vowel");
break;
case 'i':
[Link]("Vowel");
break;
case 'o':
[Link]("Vowel");
break;
case 'u':
[Link]("Vowel");
break;
default:
[Link]("Not a vowel");
break;
}
}
}
}
Output- Enter an alphabet
X
Not a vowel
Loops are used to execute one or more statements multiple times until a specified condition is
fulfilled.
There are many loops in C# such as for loop, while loop, do while loop etc. Details about these
are given as follows:
[Link] loop
The while loop continuously executes loop body until loop condition is TRUE.
If the loop condition is true, the body of the loop is executed. Otherwise, the control flow jumps
to the next statement after the while loop.
The while loop may never run if the condition is false the first time it is tested. The control skips
the loop and goes directly to the next statement.
The syntax of the while loop is given as follows:
while (condition)
{
// These statements will be executed if the condition evaluates to true
}
using System;
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
int i = 1;
[Link]("First 10 Natural Numbers are: ");
while (i <= 10)
{
[Link]( i );
i++;
}
}
}
}
2. do-while loop
The do while loop executes one or more statements multiple times as long as the loop
condition is satisfied.
It is similar to the while loop but the while loop has the test condition at the start of the loop and
the do while loop has the test condition at the end of the loop. So it executes at least once
always.
The syntax of the do while loop is given as follows:
do
{
// These statements will be executed if the condition evaluates to true
}while (condition);
using System;
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
int i = 1;
[Link]("First 10 Natural Numbers are: ");
do
{
[Link]( i );
i++;
} while (i <= 10);
}
}
}
The output of the above program is as follows:
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
3. for loop -The for loop executes one or more statements multiple times as long as the loop
condition is satisfied.
If the loop condition is true, the body of the for loop is executed. Otherwise, the control flow
jumps to the next statement after the for loop.
In For loop, first the initialization is done. This declares and initializes the loop variables, if there
are any. Then the condition is evaluated. If it is true, loop body is executed. If it is false, the
control passes to the next statement after the for loop body. After the loop body is executed, the
loop variables are updated. Then the condition is checked again and the cycle continues.
The syntax of the for loop is given below:
for ( initialization, condition, increment )
{
// These statements will be executed if the condition evaluates to true
}
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
[Link]("First 5 Natural Numbers are: ");
for (int i = 1; i <= 5; i++)
[Link](i);
}
}
}
The output of the above program is as follows:
First 10 Natural Numbers are:
1
2
3
4
5
4. Nested loops
Nested loops are loop where one loop is written inside another.
Nested loops can be created from for loops, while loops and do while loops.
The syntax of nested loops is given as follows:
Outer loop
{
// Statements in the outer loop
Inner Loop
{
// Statements in the inner loop
}
// Statements in the outer loop
}
class Example {
static void Main(string[] args) {
[Link]("Nested for loop");
for( int i = 0; i < 5; i++)
{
for(int j=0; j<=i; j++)
{ [Link](" *");
}
[Link]();
}
}
}
}
The output of the above program is as follows:
Nested for loop
*
**
***
****
*****
5. foreach loop
The foreach loop executes one or more statements for all the elements in an Array/Collection
such as List, Hash Table etc.
Syntax
foreach(datatype loopvariable in array,collectionname)
{
//body
}
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
[Link]("First 10 Natural Numbers are: ");
int[] naturalNos = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
foreach (int i in naturalNos)
{
[Link](i);
}
}
}
}
The output of the above program is as follows:
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
1)Arrays
These data elements are stored in contiguous memory locations. Some of the arrays in C# are 1-
D arrays, 2-D arrays etc. Details about these are given as follows:
1-D Arrays
One dimensional arrays consist of a single row with as many elements as required.
These arrays can be declared using the following syntax:
data_type[ ] name_of_array;
In the above syntax, data_type is the data type of array elements and name_of_array is the name
given to the array.
The syntax to create the array is given as follows:
data_type[ ] name_of_array = new data_type[array_size];
In the above syntax, data_type is the data type of array elements, name_of_array is the name
given to the array, new is a keyword that creates an instance of the array and array_size is the
size of the array.
There are many methods to assign values to the array. Some of these are given as follows:
int[ ] arr = { 1, 2, 3, 4, 5};
int[ ] arr = new int[] { 1, 2, 3, 4, 5};
int[ ] arr = new int[5] { 1, 2, 3, 4, 5};
namespace ArrayDemo
{
class Example
{
static void Main(string[ ] args)
{
int [ ] arr = new int[5] {1, 2, 3, 4, 5};
int i;
[Link]("Elements in the array are:");
for ( i = 0; i < 5; i++ )
{
[Link](arr[i]);
}
}
}
}
The output of the above program is as follows:
Elements in the array are:
1
2
3
4
5
2-D Arrays
Two dimensional arrays contain multiple rows and columns.
Each element of the 2-D array is identified as arr[i,j] where i and j are the subscripts for row
and column index respectively and the name of the array is arr.
The 2-D arrays can be declared using the following syntax:
data_type[ , ] name_of_array = new data_type[row_size, column_size];
In the above syntax, data_type is the data type of array elements, name_of_array is the name
given to the array, new is a keyword that creates an instance of the array,row_size is the number
of rows and column_size is the number of columns.
One of the methods to assign values in a 2-D array is given as follows:
int[ , ] arr = new int [2,2] { {1,4} , {8,3} };
class Example
{
static void Main(string[] args)
{
int[,] arr = new int[2, 3] { {4,1,7}, {3, 8, 2} };
int i, j;
[Link]("The elements in the 2-D array are:");
for (i = 0; i < 2; i++)
{
Array Class
The Array class is defined in the System namespace.
Using Array class, you can easily work with arrays. It is the base class for all the arrays in C#.’
IsFixedSize This property gets a value indicating whether the Array has a fixed size.
IsReadOnly This property gets a value indicating whether the Array is read-only.
Length This property gets the total number of elements in all the dimensions of the Array.
Rank This property gets the rank (number of dimensions) of the Array. For example, a one-dimensional
array returns 1, a two-dimensional array returns 2, and so on.
Methods Description
GetValue(Int32)
This method gets the value at the specified position in the one-
dimensional Array. The index is specified as a 32-bit integer.
IndexOf(Array, Object)
This method searches for the specified object and returns the index
of its first occurrence in a one-dimensional array.
Reverse(Array) This method reverses the sequence of the elements in the entire one-
dimensional Array.
SetValue(Object, Int32)
This method sets a value to the element at the specified position in the
one-dimensional Array. The index is specified as a 32-bit integer.
Sort(Array)
This method sorts the elements in an entire one-dimensional Array
using the IComparableimplementation of each element of the Array.
ToString()
This method returns a string that represents the current object.
1. Sort one dimensional array in descending order using Array Class method
using System;
namespace Demo
{
public class MyApplication
{
public static void Main(string[] args)
{
int [] arr = {34, 76, 1, 99, 68};
[Link]("The original unsorted array is: ");
foreach (int i in arr)
{
[Link](i + " ");
}
[Link](arr);
[Link](arr);
[Link]();
[Link]("The sorted array is: ");
for(int i=0; i<[Link]; i++)
{
[Link](arr[i] + " ");
}
}
}
}
The output of the above program is as follows:
The original unsorted array is: 34 76 1 99 68
The sorted array is: 99 76 68 34 1
2.A program that demonstrates the copy(, ,) method of array class is given as follows:
using System;
class Example
{
static void Main()
{
int[] arr1 = new int[5] { 45, 12, 9, 77, 34};
int[] arr2 = new int[5];
[Link](arr1, arr2, 5);
[Link]("arr1: ");
foreach (int i in arr1)
{
[Link](i + " ");
}
[Link]();
[Link]("arr2: ");
foreach (int i in arr2)
{
[Link](i + " ");
}
}
}
2)Enumeration –
Enumeration (or enum) is a value data type in C#.
It is mainly used to assign the names or string values to Numbers , that make a program easy
to read and maintain.
The main objective of enum is to define our own data types (Enumerated Data Types).
Enumeration is declared using enum keyword directly inside a namespace, class, or structure.
Syntax:
enum Enum_name { string_1...; string_2...; . . }
Example : Consider the below code for the enum. Here enum with name month is created and
its data members are the name of months like jan, feb, mar, apr, may. Now let’s try to print the
default integer values of these enums. An explicit cast is required to convert from enum type to
an integral type.
using System;
namespace ConsoleApplication1 {
class Program {
// Main Method
static void Main(string[] args)
{
Output-
The value of jan in month enum is 0
The value of feb in month enum is 1
The value of mar in month enum is 2
The value of apr in month enum is 3
The value of may in month enum is 4
3)Tuples-
A tuple in C# allows us to store elements of different data types.
var t1 = [Link](value);
class Program {
public static void Main() {
var myTuple= [Link]("Taylor", "Jack", [Link](7, 8, 9));
[Link]("The elements inside myTuple: " + myTuple);
[Link](“Nested Tuple Element=”myTuple.Item3.Item1);
}
}
Output
Here, we have a tuple (7, 8, 9) inside the myTuple tuple. This is called a nested tuple.
{
[Link](tmp.Item1+" "+tmp.Item2+" "+tmp.Item3);
}
{
return [Link](10, 20, "XYZ");
}