C#

Official website

The official website for C# is maintained by Microsoft. For comprehensive documentation, tutorials, and resources, you can visit:

https://learn.microsoft.com/en-us/dotnet/csharp/

This site provides everything you need to get started with C#, including guides, reference documentation, and community resources.

Static typing

Yes, C# is a statically typed language. This means that the type of every variable is known at compile time, and type checking is performed during compilation. Errors related to types (like assigning a value of the wrong type) are caught early, which can help reduce runtime errors.

For example, when declaring a variable:

int number = 42;

Here, the variable number is explicitly declared as an int. The compiler will enforce that only integer values can be assigned to number. If you try to assign a string to number, like below:

number = "forty-two";  // This will cause a compile-time error.

The compiler will generate an error because a string cannot be implicitly converted to an integer. Such behavior is a hallmark of statically typed languages.

Example code

Below is an example C# implementation of a binary search tree (BST) along with a method to find a value in the tree. This example includes a simple Node class, a BinarySearchTree class containing an iterative search method, as well as a short Main method to demonstrate usage.

using System;

namespace BSTSearchExample
{
    // Definition for a tree node.
    public class TreeNode
    {
        public int Value;
        public TreeNode Left;
        public TreeNode Right;

        public TreeNode(int value)
        {
            Value = value;
            Left = null;
            Right = null;
        }
    }

    // Binary search tree implementation.
    public class BinarySearchTree
    {
        public TreeNode Root;

        public BinarySearchTree()
        {
            Root = null;
        }

        // Insert a value into the BST.
        public void Insert(int value)
        {
            if (Root == null)
            {
                Root = new TreeNode(value);
            }
            else
            {
                InsertHelper(Root, value);
            }
        }

        private void InsertHelper(TreeNode node, int value)
        {
            if (value < node.Value)
            {
                if (node.Left == null)
                {
                    node.Left = new TreeNode(value);
                }
                else
                {
                    InsertHelper(node.Left, value);
                }
            }
            else // assuming duplicate values go to the right subtree
            {
                if (node.Right == null)
                {
                    node.Right = new TreeNode(value);
                }
                else
                {
                    InsertHelper(node.Right, value);
                }
            }
        }

        // Iterative search method to find a value in the BST.
        public TreeNode Find(int value)
        {
            TreeNode current = Root;
            while (current != null)
            {
                if (value == current.Value)
                {
                    return current;
                }
                else if (value < current.Value)
                {
                    current = current.Left;
                }
                else
                {
                    current = current.Right;
                }
            }
            return null; // Value not found
        }

        // Alternatively, a recursive search method.
        public TreeNode FindRecursive(int value)
        {
            return FindRecursiveHelper(Root, value);
        }

        private TreeNode FindRecursiveHelper(TreeNode node, int value)
        {
            if (node == null)
                return null;

            if (node.Value == value)
                return node;
            else if (value < node.Value)
                return FindRecursiveHelper(node.Left, value);
            else
                return FindRecursiveHelper(node.Right, value);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Build the BST
            BinarySearchTree bst = new BinarySearchTree();
            int[] values = { 50, 30, 20, 40, 70, 60, 80 };

            foreach (int value in values)
            {
                bst.Insert(value);
            }

            // Search for a value using iterative method.
            int searchValue = 40;
            TreeNode foundNode = bst.Find(searchValue);

            if (foundNode != null)
            {
                Console.WriteLine($"Value {searchValue} found in BST.");
            }
            else
            {
                Console.WriteLine($"Value {searchValue} not found in BST.");
            }

            // Search for a value using recursive method.
            searchValue = 25;
            TreeNode recursiveFoundNode = bst.FindRecursive(searchValue);

            if (recursiveFoundNode != null)
            {
                Console.WriteLine($"Value {searchValue} found in BST (recursive search).");
            }
            else
            {
                Console.WriteLine($"Value {searchValue} not found in BST (recursive search).");
            }
        }
    }
}

Explanation

  1. TreeNode Class:
    • Each TreeNode stores an integer value and references to its left and right children.
  2. BinarySearchTree Class:
    • The Insert method adds nodes to the BST following BST properties.
    • Two methods are provided for searching:
      • Find(int value): Uses an iterative approach.
      • FindRecursive(int value): Uses recursion to search for the value.
    • Both methods return the node if found, or null if not found.
  3. Program Class:
    • The Main method builds a BST using an array of sample values.
    • It then demonstrates the search operations with both iterative and recursive methods.

You can compile and run this code using any C# compiler or in an IDE (e.g., Visual Studio).