Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, May 9, 2023

Interview questions related to C# parameters.

In C#, a method is a block of code that performs a specific task. It is similar to a function in other programming languages. A method can have input parameters, perform calculations or operations, and return a value.
What is naming conventions and guidelines for C# methods and parameters?
  • Method names should be written in PascalCase, with the first letter of each word capitalized. For example, CalculateTotal or PrintMessage.
  • Parameter names should also be written in camelCase, with the first letter in lowercase and the first letter of each subsequent word capitalized. For example, firstName, lastName, or customerId.
  • Method names should be descriptive and should indicate the purpose or action of the method. Avoid using abbreviations or acronyms, and try to use verbs that convey the action being performed.
  • Parameter names should be descriptive and should indicate the type of value or information being passed into the method. Avoid using single-letter variable names or abbreviations.
  • Use consistent naming conventions throughout your codebase to make it easier to read and understand.
Here's an example of a C# method and parameter following these naming conventions:

public void PrintFullName(string firstName, string lastName)
{
    Console.WriteLine("Full name: " + firstName + " " + lastName);
}

In this example, the method name PrintFullName is written in PascalCase and conveys the action of the method. The parameter names firstName and lastName are written in camelCase and indicate the type of value being passed into the method.

What is method overloading in C#?
Method overloading is a feature in C# that allows you to define multiple methods with the same name but with different parameter lists. Each version of the method can have a different number of parameters, or different types of parameters, or both. When you call the method, the compiler chooses the correct version of the method to execute based on the number and types of arguments passed.

Here's an example of method overloading in C#:

using System;

class Calculator
{
    public int Add(int num1, int num2)
    {
        return num1 + num2;
    }

    public double Add(double num1, double num2)
    {
        return num1 + num2;
    }

    public int Add(int num1, int num2, int num3)
    {
        return num1 + num2 + num3;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Calculator calc = new Calculator();

        int sum1 = calc.Add(5, 10);          // Calls Add(int, int)
        double sum2 = calc.Add(2.5, 3.7);    // Calls Add(double, double)
        int sum3 = calc.Add(1, 2, 3);        // Calls Add(int, int, int)

        Console.WriteLine("Sum 1: " + sum1);
        Console.WriteLine("Sum 2: " + sum2);
        Console.WriteLine("Sum 3: " + sum3);
    }
}

What is a C# array parameter?

C# array parameter is a parameter that allows you to pass an array as an argument to a method or function. The array parameter is declared using the square brackets [] after the parameter type.

void PrintArray(int[] arr)
{
   for (int i = 0; i < arr.Length; i++)
   {
       Console.WriteLine(arr[i]);
   }
}

// Example usage

int[] myArray = {1, 2, 3, 4, 5};
PrintArray(myArray);

What is the purpose of the params keyword in C#? 

The params keyword in C# allows you to pass a variable number of arguments of the same type to a method or function. The params keyword must be used in conjunction with an array parameter.

void PrintNumbers(params int[] numbers)
{
   for (int i = 0; i < numbers.Length; i++)
   {
       Console.WriteLine(numbers[i]);
   }
}

// Example usage
PrintNumbers(1, 2, 3, 4, 5);
PrintNumbers(10, 20, 30);

What is the conditional operator in C#? 

The conditional operator in C# is a shorthand way of writing an if-else statement. It is represented by the ? and : symbols and allows you to test a condition and return one value if the condition is true, and a different value if the condition is false.

int x = 10;
int y = 20;

int largerNumber = x > y ? x : y;

Console.WriteLine("The larger number is: " + largerNumber);

What is a default / Optional Arguments
 the parameter in C#? 

A default parameter in C# allows you to provide a default value for a method or function parameter. If no value is passed for that parameter when the method or function is called, the default value is used instead.

void PrintMessage(string message = "Hello, world!")
{
   Console.WriteLine(message);
}

// Example usage
PrintMessage(); // Prints "Hello, world!"
PrintMessage("Goodbye!"); // Prints "Goodbye!"

What is a named parameter in C#? 

A named parameter in C# allows you to specify the name of a parameter when you pass it to a method or function. This can make your code more readable and can also allow you to pass parameters in any order you like.

void PrintDetails(string name, int age, string city)
{
   Console.WriteLine("Name: " + name);
   Console.WriteLine("Age: " + age);
   Console.WriteLine("City: " + city);
}

// Example usage
PrintDetails(name: "John", age: 30, city: "New York");
PrintDetails(city: "San Francisco", name: "Jane", age: 25);

No comments:

Post a Comment