What is a collection in C#?
A collection is a group of objects of the same type that are stored together in memory.
What are generics in C#?
What is LINQ in C#?
What are lambda expressions in C#?
How do you create a new List<T> in C#?
List<T> myList = new List<T>();
What is the difference between IEnumerable and IQueryable in C#?
How do you use LINQ to query a List<T> in C#?
var query = from item in myList
where item.Property == someValue
select item;
How do you use a lambda expression in a LINQ query in C#?
var query = myList.Where(item => item.Property == someValue);
What is the purpose of the GroupBy method in LINQ in C#?
The GroupBy method in LINQ allows developers to group objects in a collection based on a common property or key. It returns a collection of groups, where each group contains objects that have the same key.How do you use the Select method in LINQ in C#?
var query = myList.Select(item => item.Property);
What is a dictionary in C#?
How do you add an item to a dictionary in C#?
myDictionary.Add(key, value);
What is the purpose of the ToList method in LINQ in C#?
How do you sort a list in C#?
myList.Sort();
myList.Sort((x, y) => y.CompareTo(x));
What is the purpose of the FirstOrDefault method in LINQ in C#?
How do you use the Any method in LINQ in C#?
bool result = myList.Any(item => item.Property == someValue);
What is the purpose of the Where method in LINQ in C#?
How do you use the Take method in LINQ in C#?
var query = myList.Take(5);
What is the purpose of the Distinct method in LINQ in C#?
How do you use the SelectMany method in LINQ in C#?
var query = myList.SelectMany(item => item.SubItems);
Arrays have a fixed size and can't be resized once they are created, whereas lists can be resized dynamically. Lists are implemented using arrays but they provide additional methods for adding and removing elements, as well as other useful features like sorting and filtering.
How do you remove an item from a list in C#?
To remove an item from a list in C#, you can use the following code:
myList.Remove(item);
IEnumerable and ICollection in C#?
IEnumerable is an interface that provides read-only access to a collection of objects, whereas ICollection is an interface that provides both read and write access to a collection of objects. ICollection inherits from IEnumerable and adds additional methods for adding, removing, and modifying elements.
How do you convert an array to a list in C#?
To convert an array to a list in C#, you can use the following code:
List<T> myList = myArray.ToList();
How do you use the Contains method in LINQ in C#?
The Contains method in LINQ is used to determine whether a collection contains a specified element. To use the Contains method in C#, you can use the following code:
bool result = myList.Contains(item);
Can you explain the difference between IQueryable and IEnumerable in C#?
IQueryable is an interface that provides the ability to perform queries against a data source that can be translated into a query language like SQL, whereas IEnumerable provides only read-only access to a collection of objects. IQueryable inherits from IEnumerable and adds additional methods for querying a data source.
How do you use the Distinct method with a custom comparer in LINQ in C#?
How do you use the Distinct method with a custom comparer in LINQ in C#?
To use the Distinct method with a custom comparer in LINQ in C#, you can use the following code:
var query = myList.Distinct(new MyEqualityComparer());
This code will create a new collection that contains only the distinct elements of the original collection, using the specified custom comparer to compare elements for equality.
How do you use the Any method with a predicate in LINQ in C#?
To use the Any method with a predicate in LINQ in C#, you can use the following code:
bool result = myList.Any(item => item.Property == someValue);
Can you explain the difference between a HashSet and a List in C#?
A HashSet is a collection that stores unique elements, whereas a List can contain duplicate elements. HashSet provides fast lookup and insertion times but doesn't maintain the order of elements, whereas List maintains the order of elements but has slower lookup and insertion times.
How do you use the ToDictionary method in LINQ in C#?
How do you use the ToDictionary method in LINQ in C#?
The ToDictionary method in LINQ is used to create a new dictionary from a collection, using a specified key selector and value selector. To use the ToDictionary method in C#, you can use the following code:
var dictionary = myList.ToDictionary(item => item.Key, item => item.Value);
This code will create a new dictionary from the myList collection, using the Key property as the key and the Value property as the value for each element in the collection.
No comments:
Post a Comment