The Any method checks whether any of the element in a sequence satisfy a specific condition or not.
If any element satisfy the condition, true is returned.
Let us see an example.
int[] arr = {5, 7, 10, 12, 15, 18, 20};
Now, using Any() method, we will check whether any of the element in the above array is greater than 10 or not.
arr.AsQueryable().All(val => val > 5);
If any of the element satisfies the condition, then True is returned.
Let us see the complete example.
Live Demo
using System; using System.Linq; class Demo { static void Main() { int[] arr = {5, 7, 10, 12, 15, 18, 20}; // checking if any of the array elements are greater than 10 bool res = arr.AsQueryable().Any(val => val > 10); Console.WriteLine(res); } }
True
Career
Get certified by completing the course
Get StartedAdvertisements
As we already know about polymorphism and method overriding in C#. C# also provides a concept to hide the methods of the base class from derived class, this concept is known as Method Hiding. It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.
Example:
using
System;
public
class
My_Family {
public
void
member()
{
Console.WriteLine(
"Total number of family members: 3"
);
}
}
public
class
My_Member : My_Family {
public
new
void
member()
{
Console.WriteLine(
"Name: Rakesh, Age: 40 \nName: Somya, "
+
"Age: 39 \nName: Rohan, Age: 20 "
);
}
}
class
GFG {
static
public
void
Main()
{
My_Member obj =
new
My_Member();
obj.member();
}
}
Output:
Name: Rakesh, Age: 40 Name: Somya, Age: 39 Name: Rohan, Age: 20
Explanation: In the above example, My_Family is the base class and My_Member is a derived class. In both the classes we have the same name method, i.e. member() method. But in the derived class, the member() method is declared with the new keyword. When this method is called, it prints the name and the age of the family members not the total number of family members. Which means when we call the member() method with the help of the derived class object, it hides the same name method present in the base class due to the presence of the new keyword.
Now we will see what will happen if we do not use the new keyword to hide the method of a base class from the derived class. As shown in the below example, when we do not use new keyword compiler will give run the code without giving an error, but it will give warning that if you want to hide a method then uses the new keyword.
Example:
using
System;
public
class
My_Family {
public
void
member()
{
Console.WriteLine(
"Total number of family members: 3"
);
}
}
public
class
My_Member : My_Family {
public
void
member()
{
Console.WriteLine(
"Name: Rakesh, Age: 40 \nName: Somya, "
+
"Age: 39 \nName: Rohan, Age: 20 "
);
}
}
class
GFG {
static
public
void
Main()
{
My_Member obj =
new
My_Member();
obj.member();
}
}
Warning:
prog.cs(18,14): warning CS0108: `My_Member.member()’ hides inherited member `My_Family.member()’. Use the new keyword if hiding was intended
prog.cs(9,14): (Location of the symbol related to previous warning)
Output:
Name: Rakesh, Age: 40 Name: Somya, Age: 39 Name: Rohan, Age: 20
In method hiding, you can also call the hidden method of the base class in the derived class using three different ways and the ways are:
Example:
using
System;
public
class
My_Family {
public
void
member()
{
Console.WriteLine(
"Total number of family members: 3"
);
}
}
public
class
My_Member : My_Family {
public
new
void
member()
{
base
.member();
Console.WriteLine(
"Name: Rakesh, Age: 40 \nName: Somya,"
+
" Age: 39 \nName: Rohan, Age: 20"
);
}
}
class
GFG {
static
public
void
Main()
{
My_Member obj =
new
My_Member();
obj.member();
}
}
Output:
Total number of family members: 3 Name: Rakesh, Age: 40 Name: Somya, Age: 39 Name: Rohan, Age: 20
Example:
using
System;
public
class
My_Family {
public
void
member()
{
Console.WriteLine(
"Total number of family members: 2"
);
}
}
public
class
My_Member : My_Family {
public
new
void
member() {
Console.WriteLine(
"Name: Rakesh, Age: 40 "
+
"\nName: Somya, Age: 39"
);
}
}
class
GFG {
static
public
void
Main()
{
My_Member obj =
new
My_Member();
((My_Family)obj).member();
}
}
Output:
Total number of family members: 2
Note: If you try to invoke a hidden method using below syntax,
My_Member obj = new My_Family();
Here, the compiler will give an error because the object of My_Family class cannot fulfill the duties of My_Member class.
Example:
using
System;
public
class
My_Family {
public
void
member()
{
Console.WriteLine(
"Total number of family members: 2"
);
}
}
public
class
My_Member : My_Family {
public
new
void
member() {
Console.WriteLine(
"Name: Rakesh, Age: 40 "
+
"\nName: Somya, Age: 39"
);
}
}
class
GFG {
static
public
void
Main()
{
My_Family obj =
new
My_Member();
obj.member();
}
}
Output:
Total number of family members: 2
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Previous: LINQ (Language Integrated Query)
Next: What Is Sticky Sublimation Paper And Why Do You Need It?
Comments
Please Join Us to post.
0