C# Interview Questions

36 Questions
C# Programming

C# Programming

Web DevelopmentFrontendBackendGame Dev

Question 13

Explain what LINQ is and give an example.

Answer:

LINQ (Language Integrated Query) is a powerful feature in C# and .NET that provides a consistent and readable way to query and manipulate data from various data sources such as collections, databases, XML documents, and more. LINQ enables developers to write queries directly in C# (or other .NET languages) using familiar syntax, which improves code readability and maintainability.

Key Features of LINQ

  1. Uniform Syntax: LINQ provides a uniform syntax for querying different data sources. The same syntax can be used to query collections, databases, XML, and more.
  2. Strong Typing: LINQ queries are strongly typed, which means errors can be caught at compile time rather than at runtime.
  3. IntelliSense Support: Visual Studio provides IntelliSense support for LINQ queries, making it easier to write and understand queries.
  4. Integration with C#: LINQ queries are integrated directly into the C# language, allowing for seamless use within the language.

LINQ Query Syntax

LINQ provides two main query syntaxes:

  1. Query Syntax: Similar to SQL-like syntax.
  2. Method Syntax: Uses method chaining with lambda expressions.

Example of LINQ

Query Syntax Example

Here’s an example using LINQ with a list of integers to find even numbers:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        // Sample data source
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // LINQ query using query syntax to find even numbers
        var evenNumbers = from num in numbers
                          where num % 2 == 0
                          select num;

        Console.WriteLine("Even numbers:");
        foreach (var num in evenNumbers)
        {
            Console.WriteLine(num);
        }
    }
}

In this example:

  • The data source is a list of integers (numbers).
  • The LINQ query evenNumbers selects numbers from the list that are even.
  • The result is printed to the console.

Method Syntax Example

Here’s the same example using method syntax:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        // Sample data source
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // LINQ query using method syntax to find even numbers
        var evenNumbers = numbers.Where(num => num % 2 == 0);

        Console.WriteLine("Even numbers:");
        foreach (var num in evenNumbers)
        {
            Console.WriteLine(num);
        }
    }
}

In this example:

  • The Where method is used with a lambda expression num => num % 2 == 0 to filter even numbers.
  • The result is printed to the console.

LINQ with Complex Objects

LINQ can also be used with complex objects such as collections of custom types. Here’s an example using a list of Person objects:

using System;
using System.Collections.Generic;
using System.Linq;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program
{
    public static void Main()
    {
        // Sample data source
        List<Person> people = new List<Person>
        {
            new Person { Name = "John", Age = 30 },
            new Person { Name = "Jane", Age = 25 },
            new Person { Name = "Tom", Age = 35 },
            new Person { Name = "Lucy", Age = 22 }
        };

        // LINQ query to find people aged 30 or older
        var adults = from person in people
                     where person.Age >= 30
                     select person;

        Console.WriteLine("Adults:");
        foreach (var person in adults)
        {
            Console.WriteLine($"{person.Name}, Age {person.Age}");
        }
    }
}

In this example:

  • The data source is a list of Person objects.
  • The LINQ query adults selects people aged 30 or older.
  • The result is printed to the console.

Summary

LINQ (Language Integrated Query) is a powerful tool in C# that allows developers to write queries directly within their code, providing a consistent and readable way to query various data sources. With its strong typing, IntelliSense support, and integration with the C# language, LINQ simplifies data manipulation and improves code maintainability.

Recent job openings