Foreach Loop in C# (part 2)
Important Points:
🔸 The foreach loop iterate only in forward direction.
🔸 Performance wise foreach loop takes much time as compared with for loop.
🔸 Because internally it uses extra memory space as well as.
🔸 The foreach loop use
🔸 Exit the foreach loop by using break, return, Goto and throw.
The following example demonstrates the foreach loop on a dictionary collection.
Important Points:
🔸 The foreach loop iterate only in forward direction.
🔸 Performance wise foreach loop takes much time as compared with for loop.
🔸 Because internally it uses extra memory space as well as.
🔸 The foreach loop use
GetEnumarator()
method of the IEnumerable
interface. So, the foreach loop can be used with any class that has implemented the interface.🔸 Exit the foreach loop by using break, return, Goto and throw.
The following example demonstrates the foreach loop on a dictionary collection.
Foreach Loop in C# (part 3)
As mentioned before, the foreach loop can be used to iterate any class that has implemented the
Above, the
As mentioned before, the foreach loop can be used to iterate any class that has implemented the
IEnumerable
interface. The following example demonstrates how to implement the IEnumerable
interface in order to use the foreach loop with the custom class.Above, the
Shop
class has implemented the IEnumerable
interface that contains the GetEnumerator()
method. This will enable the Shop
class to be used with the foreach loop that returns the Customer
objects.Foreach Loop in C# (part 4)
Beginning with C# 8.0, you can use the
You can also use the
🔸 A type has the public parameterless
🔸 The return type of the
By default, stream elements are processed in the captured context. If you want to disable capturing of the context, use the TaskAsyncEnumerableExtensions.ConfigureAwait extension method. For more information about synchronization contexts and capturing the current context, see Consuming the Task-based asynchronous pattern. For more information about asynchronous streams, see the Asynchronous streams section of the What's new in C# 8.0 article.
💬 Support the channel and its mission to help programmers learn C#: https://www.patreon.com/csharp1001notes
Beginning with C# 8.0, you can use the
await foreach
statement to consume an asynchronous stream of data, that is, the collection type that implements the IAsyncEnumerable<T> interface. Each iteration of the loop may be suspended while the next element is retrieved asynchronously. The following example shows how to use the await foreach
statement:You can also use the
await foreach
statement with an instance of any type that satisfies the following conditions:🔸 A type has the public parameterless
GetAsyncEnumerator
method. That method can be a type's extension method.🔸 The return type of the
GetAsyncEnumerator
method has the public Current
property and the public parameterless MoveNextAsync
method whose return type is Task<bool>
, ValueTask<bool>
, or any other awaitable type whose awaiter's GetResult
method returns a bool
value.By default, stream elements are processed in the captured context. If you want to disable capturing of the context, use the TaskAsyncEnumerableExtensions.ConfigureAwait extension method. For more information about synchronization contexts and capturing the current context, see Consuming the Task-based asynchronous pattern. For more information about asynchronous streams, see the Asynchronous streams section of the What's new in C# 8.0 article.
💬 Support the channel and its mission to help programmers learn C#: https://www.patreon.com/csharp1001notes
How to loop through an enum in C#? (part 1)
Here you will learn how to enumerate or loop through an enum.
In C#, an enum is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example,
An enum can be looped through using
The following example gets the names of an enum using the
Here you will learn how to enumerate or loop through an enum.
In C#, an enum is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example,
WeekDays.Monday
is more readable than number 0 when referring to the day in a week.An enum can be looped through using
Enum.GetNames<TEnum>()
, Enum.GetNames()
, Enum.GetValues<TEnum>()
, or Enum.GetValues()
static methods with the foreach loop.The following example gets the names of an enum using the
Enum.GetNames<TEnum>()
method.How to loop through an enum in C#? (part 2)
The
The following example shows how to get the values of an enum using the
💬 The channel has been running since 2018. It needs your support: https://www.patreon.com/csharp1001notes
The
Enum.GetValues<TEnum>()
is a static method that retrieves an array of the constant values of the specified enum.The following example shows how to get the values of an enum using the
Enum.GetValues<TEnum>()
method.💬 The channel has been running since 2018. It needs your support: https://www.patreon.com/csharp1001notes
NullReferenceException in C# (part 1)
The NullReferenceException is an exception that will be thrown while accessing a null object.
The following example shows the code that throws the
In the above example, a
The NullReferenceException is an exception that will be thrown while accessing a null object.
The following example shows the code that throws the
NullReferenceException
:In the above example, a
NullReferenceException
will be thrown in the DisplayCities()
function while accessing cities list using a foreach loop because the cities list is null. If the caller of the DisplayCities()
function pass a null IList
value then it will raise a NullReferenceException
.Solutions to fix the NullReferenceException (part 2)
To prevent the
Solution 1: Check whether an object contains a null value or not using an if condition, as shown below:
In the above example,
To prevent the
NullReferenceException
exception, check whether the reference type parameters are null or not before accessing them.Solution 1: Check whether an object contains a null value or not using an if condition, as shown below:
In the above example,
if(cities == null)
checks whether the cities object is null or not. If it is null then display the appropriate message and return from the function.Solutions to fix the NullReferenceException (part 3)
In .NET 5, use the null conditional operator
In the above example,
In .NET 5, use the null conditional operator
?.
, as shown below:In the above example,
std?.FirstName
is like if(std != null) std.FirstName
. The std?.FirstName
checks if std
is not null then only access the FirstName
property.Solutions to fix the NullReferenceException (part 4)
In .NET 4.x and above versions, use Null-Coalescing operator
In the above example,
💬 Like this note? Want to support the channel? Become a patron: https://www.patreon.com/csharp1001notes
In .NET 4.x and above versions, use Null-Coalescing operator
??
to prevent an exception, as shown below:In the above example,
??
is a null coalescing operator that checks if an object is null or not, if it is null then create an object. The cities ?? new List<string>()
creates a new list object if a cities
is null in foreach loop. Thus, the NullReferenceException
will be not be raised.💬 Like this note? Want to support the channel? Become a patron: https://www.patreon.com/csharp1001notes
Generate Random Numbers in C# (part 1)
Here you will learn how to generate random numbers in C#.
C# provides the Random class to generate random numbers based on the seed value. Use the following methods of the Random class to generate random numbers.
The following example demonstrates how to generate a random integers.
Call the
Here you will learn how to generate random numbers in C#.
C# provides the Random class to generate random numbers based on the seed value. Use the following methods of the Random class to generate random numbers.
The following example demonstrates how to generate a random integers.
Call the
Next()
method multiple times to get the multiple random numbers, as shown below.Generate Random Numbers in Range
Use the
The following example generates the positive random numbers that are less than 10.
Use the
Next(int)
method overload to generate a random integer that is less than the specified maximum value.The following example generates the positive random numbers that are less than 10.
Generate Random Number in Min to Max Range
Use the
In the above example,
Use the
Next(int min, int max)
overload method to get a random integer that is within a specified range.In the above example,
rnd.Next(10, 20)
generate random numbers that will be between 10 to 19.Generate Random Floating Point Number
Use the
Use the
NextDouble()
method to get a random floating-point number between 0.0 to 1.0, as shown below.Seed Value
The
Two different instances of the
In the above example, two different objects of the Random class are instantiated with the same seed value. So, they will generate the same random numbers.
By default, the seed value is time-dependent. In .NET Core, the default seed value is produced by the thread-static, pseudo-random number generator.
💬 If you appreciate this channel and want it to grow, why not support it: https://www.patreon.com/csharp1001notes
The
Random
class uses the seed value as a starting value for the pseudo-random number generation algorithm. By default, the Random
class uses the system clock to generate its seed value so that each instance of the Random
class can generate different random numbers.Two different instances of the
Random
class having the same seed value will generate the same random numbers, as shown below.In the above example, two different objects of the Random class are instantiated with the same seed value. So, they will generate the same random numbers.
By default, the seed value is time-dependent. In .NET Core, the default seed value is produced by the thread-static, pseudo-random number generator.
💬 If you appreciate this channel and want it to grow, why not support it: https://www.patreon.com/csharp1001notes
Default Value of Auto-Implemented Property
In C# 6.0 or higher versions, assign the inline default value to the auto-implemented property, as shown below.
In C# 6.0 or higher versions, assign the inline default value to the auto-implemented property, as shown below.
Using DefaultValue Attribute
You can assign the default value using the DefaultValueAttribute attribute, as shown below.
💬 Support the channel and its mission to help programmers learn C#: https://www.patreon.com/csharp1001notes
You can assign the default value using the DefaultValueAttribute attribute, as shown below.
💬 Support the channel and its mission to help programmers learn C#: https://www.patreon.com/csharp1001notes
In C#, the Scope of the variable determines the accessibility of the variable to a particular part of the application. Variables can be declared within the class, method, and code block of a loop, condition, etc.
There are three types of scopes in C#.
- Class Level Scope
- Method Level Scope
- Code-Block Level Scope
Class Level Scope
A variable declared within a class is known as a field. It has a class-level scope that can be accessed anywhere in the class, such as class methods, properties, etc.
In the above example, the
The class level variables can also be accessed out of class using class objects depending on the access modifiers. The static variables of the class can only be accessed from the static methods or properties.
There are three types of scopes in C#.
- Class Level Scope
- Method Level Scope
- Code-Block Level Scope
Class Level Scope
A variable declared within a class is known as a field. It has a class-level scope that can be accessed anywhere in the class, such as class methods, properties, etc.
In the above example, the
Student
class contains two class variables (a.k.a. fields) _firstName
and _lastName
. These fields can be accessed anywhere within the class, i.e., within any non-static methods and properties.The class level variables can also be accessed out of class using class objects depending on the access modifiers. The static variables of the class can only be accessed from the static methods or properties.
Method Level Scope
A variable declared within a method has a method level Scope. It is also known as a local variable of the method where it is declared. It cannot be accessed outside the method.
Any nested code blocks within the method can access this type of variable. It cannot be declared twice with the same name within the same scope.
The local variable's scope ends when the method execution completes, and they will be collected by the garbage collector.
The following example shows the method Level scope.
In the above example, the
A variable declared within a method has a method level Scope. It is also known as a local variable of the method where it is declared. It cannot be accessed outside the method.
Any nested code blocks within the method can access this type of variable. It cannot be declared twice with the same name within the same scope.
The local variable's scope ends when the method execution completes, and they will be collected by the garbage collector.
The following example shows the method Level scope.
In the above example, the
Main()
method can only access variables declared in the Main()
method but not variables of other methods. In the same way, the Process()
method cannot access variables declared in the Main()
or any other method.Code-Block Level Scope
A variable declared within a loop or any block within brackets has the code-block level scope. A variable declared within a loop or code block cannot be accessed outside of it, whereas a variable declared outside of the loop can be accessible within the loop.
In the above example, a variable
A variable must be declared outside of the code block to make it accessible to outside code.
💬 The channel has been running since 2018. It needs your support: https://www.patreon.com/csharp1001notes
A variable declared within a loop or any block within brackets has the code-block level scope. A variable declared within a loop or code block cannot be accessed outside of it, whereas a variable declared outside of the loop can be accessible within the loop.
In the above example, a variable
i
declared within a for loop. So, it can only be accessed within the for
loop block and cannot be accessed outside for loop. In the same way, x
is declared within the if block, so it can only be accessed in that block but not outside of it.A variable must be declared outside of the code block to make it accessible to outside code.
💬 The channel has been running since 2018. It needs your support: https://www.patreon.com/csharp1001notes