C#



1. _________ are reserved, and cannot be used as identifiers.  

  • A) Keywords
  • B) Identifiers
  • C) Literals
  • D) Variables
  • E) None of these


2. What is C#?  

C# is an object oriented, type safe and managed language that is compiled by .Net framework to generate Microsoft Intermediate Language.


3. Describe Strong Programming Features of C#.  

Although C# constructs closely follow traditional high-level languages C and C++ and being an object-oriented programming language, it has strong resemblance with Java, it has numerous strong programming features that make it endearing to multitude of programmers worldwide.

Following is the list of few important features:

  • Boolean Conditions

  • Automatic Garbage Collection

  • Standard Library

  • Assembly Versioning

  • Properties and Events

  • Delegates and Events Management

  • Easy-to-use Generics

  • Indexers

  • Conditional Compilation

  • Simple Multithreading

  • LINQ and Lambda Expressions

  • Integration with Windows



4. Describe C# in brief.  

C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by Ecma and ISO.

C# was developed by Anders Hejlsberg and his team during the development of .Net Framework.

C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages to be used on different computer platforms and architectures.

The following reasons make C# a widely used professional language:

  • Modern, general-purpose programming language

  • Object oriented.

  • Component oriented.

  • Easy to learn.

  • Structured language.

  • It produces efficient programs.

  • It can be compiled on a variety of computer platforms.

  • Part of .Net Framework.




5. Integrated Development Environment (IDE) For C#.  

Microsoft provides the following development tools for C# programming:

  • Visual Studio 2010 (VS)

  • Visual C# 2010 Express (VCE)

  • Visual Web Developer

The last two are freely available from Microsoft official website. Using these tools, you can write all kinds of C# programs from simple command-line applications to more complex applications. You can also write C# source code files using a basic text editor, like Notepad, and compile the code into assemblies using the command-line compiler, which is again a part of the .NET Framework.

Visual C# Express and Visual Web Developer Express edition are trimmed down versions of Visual Studio and has the same look and feel. They retain most features of Visual Studio. In this tutorial, we have used Visual C# 2010 Express.

You can download it from Microsoft Visual Studio. It gets automatically installed in your machine. Please note that you need an active internet connection for installing the express edition.



6. Writing C# Programs on Linux or Mac OS  

Although the.NET Framework runs on the Windows operating system, there are some alternative versions that work on other operating systems. Mono is an open-source version of the .NET Framework which includes a C# compiler and runs on several operating systems, including various flavors of Linux and Mac OS. Kindly check Go Mono.

The stated purpose of Mono is not only to be able to run Microsoft .NET applications cross-platform, but also to bring better development tools to Linux developers. Mono can be run on many operating systems including Android, BSD, iOS, Linux, OS X, Windows, Solaris and UNIX.



7. What are the components of c# programming ?  

A C# program basically consists of the following parts:

  • Namespace declaration

  • A class

  • Class methods

  • Class attributes

  • A Main method

  • Statements & Expressions

  • Comments

Let us look at a simple code that would print the words "Hello World":

using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         /* my first program in C# */
         Console.WriteLine("Hello World");
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following result:

Hello World

Let us look at various parts of the above program:

  • The first line of the program using System; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.

  • The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.

  • The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally would contain more than one method. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.

  • The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class will do when executed

  • The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program.

  • The Main method specifies its behavior with the statement Console.WriteLine("Hello World");

    WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen.

  • The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

It's worth to note the following points:

  • C# is case sensitive.

  • All statements and expression must end with a semicolon (;).

  • The program execution starts at the Main method.

  • Unlike Java, file name could be different from the class name.



8. How Does a C# Application Run?  

An important point is that C# is a "managed" language, meaning that it requires the .NET Common Language Runtime (CLR) to execute. Essentially, as an application that is written in C# executes, the CLR is managing memory, performing garbage collection, handling exceptions, and providing many more services that you, as a developer, don't have to write code for. The C# compiler produces Intermediate Language (IL) , rather than machine language, and the CLR understands IL. When the CLR sees the IL, it Just-In-Time (JIT) compiles it, method by method, into compiled machine code in memory and executes it. As mentiond previously, the CLR manages the code as it executes.

Because C# requires the CLR, you must have the CLR installed on your system. All new Windows operating systems ship with a version of the CLR and it is available via Windows Update for older systems. The CLR is part of the .NET, so if you see updates for the .NET Framework Runtime, it contains the CLR and .NET Framework Class Library (FCL). It follows that if you copy your C# application to another machine, then that machine must have the CLR installed too.



9. What are advantages of C# ?  

XML documentation is generated from source code but this is now been incorporated in Whidbey.
  • Operator overloading which is not in current VB.NET but is been introduced in Whidbey.
  • Use of this statement makes unmanaged resource disposal simple.
  • Access to Unsafe code. This allows pointer arithmetic etc, and can improve performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies).This is the major difference that you can access unmanaged code in C# and not in VB.NET.


10. What are Static(C#) variables?  

Static classes are used when a class provides functionality, which is not specific to any instance. In short, if you want an object to be shared between multiple instances you will use a static/Shared class.


11. Describe data-types in C#.  

Keyword

Type

Values

sbyte

Numeric-Integer

-128 to 127

byte

Numeric-Integer

0 to 255

short

Numeric-Integer

-32,768 to 32,767

ushort

Numeric-Integer

0 to 65,535

int

Numeric – Integer

-2 147 483 648 to 2 147 483 647

uint

Numeric-Integer

0 to 4,294,967,295

long

Numeric-Integer

- 9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

ulong

Numeric-Integer

      0 to 18,446,744,073,709,551,615

float

Numeric – Real

1.5 x 10-45 to 3.4 x 1038

double

Numeric – Real

5.0 x 10-324 to 1.7 x 10308

decimal

Numeric-Real

1.0 x 10-28 to 7.9 x 1028

char

Character

All Unicode characters

string

Unicode string

A Unicode string with no special upper bound

bool

Boolean

True or False

object

Pointer to an object

Any objects

DateTime

Date/time

Date or time value



12. Explain Structure.  


13. Explain Constructors of C# structure.  

In c#, a structure can have constructors. A constructor is a special member of the structure. It can be used to initialize variables of the structure when its object is created. The name of constructor must be the same as the name of structure. The constructor called automatically when the keyword "new" is used to create the objects of the structure.
Example:
struct student
 {
    public string stnumber;
    public string stname;
    public string stsex;
    public student(string id,string name,string sex){ //constructor
            stnumber=id;
            stname=name;
            stsex=sex;
       }
 }

student st=new student("s001","Dara Yuk","M"); //create object
Console.Write("{0}\t{1}\t{2}",st.stnumber,st.name,st.sex);

Note: a structure in C# can have other members such as methods, properties, and events. However, if you have these kinds of things in your program, you should consider using class instead.



14. Explain C# enum.  

An enum is a collection of named constants called emumerator list. The type of an enum must be integral type (byte, sbyte, short, ushort, int, uint, long, ulong). To declare an enum, you can use enum keyword followed by its name Example: enum WeekDays{Mon, Tue, Wed, Thu, Fri, Sat, Sun}; You can also specify the type the enum by placing the type after the semi-coln. Example: enum WeekDays : int {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; By default, the elements of the enum store values starting from 0 upward(Mon=0, Tue=1,…,Sun=6). If you do not want to accept the default values, you can change them as you like. In the case below, the values start from 1 instead of 0. enum WeekDays{Mon=1, Tue, Wed, Thu, Fri, Sat, Sun}; To access the values of the enum WeekDays, you can do as the following: int x=(int)WeekDays.Mon; Console.WriteLine("Mon={0}",x);


15. What is the difference between public, static and void?  

All these are access modifiers in C#. 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. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created. And Void is a type modifier that states that the method or variable does not return any value.


16. What is the difference between ref & out parameters?  

An argument passed as ref must be initialized before passing to the method whereas out parameter needs not to be initialized before passing to a method.


17. What is the use of using statement in C#?  

The using block is used to obtain a resource and use it and then automatically dispose of when the execution of block completed.


18. Can “this” be used within a static method?  

We can’t use ‘This’ in a static method because we can only use static variables/methods in a static method.


19. What is difference between constants and read-only?  

Constant variables are declared and initialized at compile time. The value can’t be changed after wards. Read-only variables will be initialized only from the Static constructor of the class. Read only is used only when we want to assign the value at run time.


20. Can a private virtual method be overridden?  

No, because they are not accessible outside the class.


Java Interview Question

.Net Interview Question

PHP Interview Question

AngularJS Interview Questions