/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
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:
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
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 runOutput
3 2
Sources
Related pages
errors · csharpC# error CS0103: The name does not exist in the current contextC# CS0103 means a name is not visible where it is used, usually a variable declared inside an if or for block. Declare it in the outer scope. Real output.errors · csharpC# error CS0246: The type or namespace name could not be foundCS0246 means the C# compiler cannot resolve a type or namespace: a missing using directive, NuGet package or project reference. Real dotnet build output.errors · csharpC# warning CS8618: Non-nullable property must contain a non-null value when exiting constructorCS8618 warns that a non-nullable property or field can still be null after the constructor runs. Fix it with required, an initializer, a constructor or string?.commands · csharpc# sort a list of objects by property (OrderBy, ThenBy, List.Sort)Sort a List of objects by a property in C# with LINQ OrderBy, OrderByDescending and ThenBy, or in place with List.Sort. Stability and case-insensitive order.