{"id":1748,"library":"tree-sitter-c-sharp","title":"Tree-sitter C# Grammar","description":"The `tree-sitter-c-sharp` library provides a C# grammar definition for the `tree-sitter` universal parsing system. It allows Python applications to parse C# source code into a concrete syntax tree (CST) efficiently. The current version is 0.23.1, and releases typically track updates to the C# language specification and bug fixes for the grammar.","status":"active","version":"0.23.1","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-c-sharp","tags":["tree-sitter","parsing","grammar","csharp","ast","syntax-tree"],"install":[{"cmd":"pip install tree-sitter tree-sitter-c-sharp","lang":"bash","label":"Install with core library"}],"dependencies":[{"reason":"Provides the core `tree_sitter` Python bindings required to load and use the C# grammar.","package":"tree-sitter","optional":false}],"imports":[{"note":"The C# grammar is exposed directly from the `tree_sitter_c_sharp` package, not a submodule of `tree_sitter.languages`.","wrong":"from tree_sitter.languages import csharp_grammar","symbol":"language","correct":"import tree_sitter_c_sharp\nfrom tree_sitter import Language, Parser"},{"symbol":"Language","correct":"from tree_sitter import Language"},{"symbol":"Parser","correct":"from tree_sitter import Parser"}],"quickstart":{"code":"import tree_sitter_c_sharp\nfrom tree_sitter import Language, Parser\n\n# Load the C# grammar from the installed package\nC_SHARP_LANGUAGE = Language(tree_sitter_c_sharp.language())\n\n# Create a parser instance\nparser = Parser()\nparser.set_language(C_SHARP_LANGUAGE)\n\n# C# source code to parse\ncode = \"\"\"\nusing System;\n\nnamespace MyNamespace\n{\n    class MyClass\n    {\n        static void Main(string[] args)\n        {\n            Console.WriteLine(\"Hello, Tree-sitter C#!\");\n        }\n    }\n}\n\"\"\"\n\n# Parse the code\ntree = parser.parse(bytes(code, \"utf8\"))\n\n# Print a snippet of the AST\nroot_node = tree.root_node\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Number of children: {len(root_node.children)}\")\nif root_node.children:\n    first_child = root_node.children[0]\n    print(f\"First child type: {first_child.type}\")\n    print(f\"First child text: {first_child.text.decode('utf8')}\")\n","lang":"python","description":"This quickstart demonstrates how to load the C# grammar provided by `tree-sitter-c-sharp`, create a `tree_sitter.Parser`, and parse a simple C# code snippet into a syntax tree. It then accesses basic information from the root node."},"warnings":[{"fix":"Ensure both `pip install tree-sitter` and `pip install tree-sitter-c-sharp` are executed.","message":"The `tree-sitter` Python library (PyPI package `tree-sitter`) is a required dependency but is NOT automatically installed with `tree-sitter-c-sharp`. You must install both separately.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use the latest stable version of `tree-sitter-c-sharp` to ensure the broadest support for modern C# syntax. Regularly update the package for C# language compatibility.","message":"The C# grammar evolves to support new language features (e.g., C# 10, C# 11). Older versions of `tree-sitter-c-sharp` might not correctly parse syntax from newer C# language versions, leading to `ERROR` nodes in the parse tree.","severity":"gotcha","affected_versions":"All versions, especially when using older grammar versions with newer C# code."},{"fix":"Encode your source code string to bytes, typically using UTF-8: `parser.parse(bytes(code_string, 'utf8'))`.","message":"When working with `tree-sitter`, always pass byte strings (e.g., `bytes(code, 'utf8')`) to the parser's `parse` method. Passing a regular Python string will raise a `TypeError`.","severity":"gotcha","affected_versions":"All versions of `tree-sitter` library."}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}