Skip to content

/errors/csharp/cs1061-does-not-contain-a-definition

C# error CS1061: does not contain a definition and no accessible extension method

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 10.0.401updated
tool
C#
exit code
1
versions
.NET 6 – 10, C# 10+
os
any

error CS1061: 'T' does not contain a definition for 'M' and no accessible extension method 'M' accepting a first argument of type 'T' could be found means you called a method or property M on an expression of type T, and neither T nor any imported extension method has a member with that name. Read the type in the message: it is often not the type you expected. Fix the member name, cast to the right type, or add the using for the extension method's namespace (System.Linq for Where, Select, ToList).

Reproduce it

bash
export DOTNET_NOLOGO=1 DOTNET_CLI_TELEMETRY_OPTOUT=1
dotnet new console -o app >/dev/null 2>&1
cd app
cat > Program.cs <<'EOF'
int[] values = { 1, 2, 3 };
Console.WriteLine(values.Length2);
EOF
dotnet build -nologo 2>&1 | grep -o 'error CS[0-9]*: .*' | sed 's/ \[[^]]*\]$//' | sort -u
exit ${PIPESTATUS[0]}

Output · exit 1

error CS1061: 'int[]' does not contain a definition for 'Length2' and no accessible extension method 'Length2' accepting a first argument of type 'int[]' could be found (are you missing a using directive or an assembly reference?)

Missing using for an extension method

dotnet new console projects turn on ImplicitUsings, which imports System.Linq for you. With it off, LINQ methods fail with the same code:

bash
export DOTNET_NOLOGO=1 DOTNET_CLI_TELEMETRY_OPTOUT=1
dotnet new console -o app >/dev/null 2>&1
cd app
sed -i 's#<ImplicitUsings>enable</ImplicitUsings>#<ImplicitUsings>disable</ImplicitUsings>#' app.csproj
cat > Program.cs <<'EOF'
using System;
int[] values = { 1, 2, 3 };
Console.WriteLine(values.Where(v => v > 1).Count());
EOF
dotnet build -nologo 2>&1 | grep -o 'error CS[0-9]*: .*' | sed 's/ \[[^]]*\]$//' | sort -u
exit ${PIPESTATUS[0]}

Output · exit 1

error CS1061: 'int[]' does not contain a definition for 'Where' and no accessible extension method 'Where' accepting a first argument of type 'int[]' could be found (are you missing a using directive or an assembly reference?)

Fix

bash
export DOTNET_NOLOGO=1 DOTNET_CLI_TELEMETRY_OPTOUT=1
dotnet new console -o app >/dev/null 2>&1
cd app
sed -i 's#<ImplicitUsings>enable</ImplicitUsings>#<ImplicitUsings>disable</ImplicitUsings>#' app.csproj
cat > Program.cs <<'EOF'
using System;
using System.Linq;
int[] values = { 1, 2, 3 };
Console.WriteLine(values.Length);
Console.WriteLine(values.Where(v => v > 1).Count());
EOF
dotnet run

Output

3
2

Sources