Programming for Automation
C# Interview Questions & Answers
C# interview questions for automation testers: OOP, value vs reference types, LINQ, async/await, NUnit/xUnit, Selenium C#, Playwright .NET and BDD.
Interview Questions
Showing 15 of 50 questions
Page classes encapsulate locators behind methods, test classes inherit a BaseTest for setup/teardown, IWebDriver gives polymorphism across browsers, and interfaces/abstract classes provide abstraction.
Value types (int, bool, struct, enum) hold their data directly and are copied on assignment. Reference types (class, string, arrays, interfaces) hold a reference to an object, so copies point to the same instance.
Properties expose fields through get/set accessors with optional logic, and auto-properties let the compiler generate the backing field. In page objects, expression-bodied properties are a clean way to expose elements.
A class can implement many interfaces but inherit only one abstract class. Abstract classes can have fields, constructors and implemented members; interfaces define contracts and since C# 8 can include default implementations.
string is immutable, so each concatenation creates a new object. StringBuilder is mutable and efficient when building strings in loops, such as large logs or report bodies.
Use try/catch for specific exceptions, finally for cleanup, and exception filters with when for conditional handling. In tests, let unexpected exceptions fail the test and use Assert.Throws to verify expected ones.
List<T> for ordered items, Dictionary<TKey,TValue> for test data and config, HashSet<T> for uniqueness, and ReadOnlyCollection<IWebElement> which Selenium's FindElements returns.
LINQ queries collections declaratively with Select, Where, OrderBy, Any and more. It's ideal for extracting element text, validating sort order and filtering API response data.
async/await lets methods run asynchronous I/O without blocking threads; await pauses until the Task completes. Playwright .NET's API is fully async, so tests and page objects return Task and await every call.
All three are .NET test frameworks. NUnit uses [Test], [SetUp], [TestCase] and is most common for Selenium; xUnit uses [Fact]/[Theory] with constructor/IDisposable for setup; MSTest uses [TestMethod], [TestInitialize] and [DataRow].
Create the driver in [SetUp] and quit it in [TearDown] in a BaseTest class. For parallel runs, store it in ThreadLocal<IWebDriver> or use [FixtureLifeCycle(LifeCycle.InstancePerTestCase)].
Use WebDriverWait with a lambda condition, since ExpectedConditions moved out of the core package. Configure IgnoreExceptionTypes for transient exceptions.
Install Microsoft.Playwright.NUnit (or MSTest/xUnit), inherit PageTest to get a Page per test, and use async locators with auto-waiting plus Expect() web-first assertions.
Load settings from appsettings.json and environment variables with Microsoft.Extensions.Configuration, bind them to a typed class, and register services like driver factories and API clients in a DI container.
Reqnroll is the open-source successor to SpecFlow, which reached end of life. Gherkin feature files describe behavior and step definition classes bind each Given/When/Then to C# methods, running on NUnit, xUnit or MSTest.