1. What is C#?
C# (C-Sharp) is a programming language developed by Microsoft that runs on the .NET
Framework. C# is used to develop web apps, desktop apps, mobile apps, games and much
more. It is an Object Oriented Language.
2. Explain types of comment in C# with examples
Single line
Example:
//This is a single line comment
ii. Multiple line (/* */)
Example:
/*This is a multiple line comment
We are in line 2
Last line of comment*/
iii. XML Comments (///).
Eg:
/// summary;
/// Set error message for multilingual language.
/// summary
Q) what is C# Exception?
When executing C# code, different errors can occur: coding errors made by
the programmer, errors due to wrong input, or other unforeseeable things.
When an error occurs, C# will normally stop and generate an error message.
The technical term for this is: C# will throw an exception (throw an error).
Q) What is Try and Catch in Csharp
A)
The try statement allows you to define a block of code to be tested for errors
while it is being executed.
The catch statement allows you to define a block of code to be executed, if
an error occurs in the try block.
The try and catch keywords come in pairs:
B)
C# try and catch
The try statement allows you to define a block of code to be tested for errors while it
is being executed. The catch statement allows you to define a block of code to be
executed, if an error occurs in the try block.
try
// Block of code to try
catch (Exception e)
// Block of code to handle errors
Q) What is finally?
The finally statement lets you execute code, after try...catch, regardless
of the result:
try
int[] myNumbers = {1, 2, 3};
Console.WriteLine(myNumbers[10]);
}
catch (Exception e)
Console.WriteLine("Something went wrong.");
finally
Console.WriteLine("The 'try catch' is finished.");
Q). Can multiple catch blocks be executed?
No, Multiple catch blocks of similar type can't be executed. Once the proper
catch code executed, the control is transferred to the finally block, and then the
code that follows the finally block gets executed.
Q. What is the difference between public, static, and void?
Public declared variables or methods are accessible anywhere in the
application.
Static declared variables or methods are globally accessible without creating an
instance of the class.
Void is a type modifier that states that the method or variable does not return
any value.
Q) What is an object?
An object is an instance of a class through which we access the methods of that
class. "New" keyword is used to create an object. A class that creates an object
in memory will contain the information about the methods, variables, and
behavior of that class.
class Car
string color = "red";
static void Main(string[] args)
Car myObj = new Car();
Console.WriteLine(myObj.color);
Q) What is class Member
Fields and methods inside classes are often referred to as "Class Members":
Create a Car class with three class members: two fields and one method.
// The class
class MyClass
// Class members
string color = "red"; // field
int maxSpeed = 200; // field
public void fullThrottle() // method
Console.WriteLine("The car is going as fast as it can!");
}
https://www.guru99.com/c-sharp-interview-questions.html