c# interview questions and answers

1. Please program a bubble sorting algorithm?

public static void bubble_sort(int[] x)

        {

            for (int i = 0; i < x.Length; i++)

            {

                for (int j = i; j < x.Length; j++)

                {

                    if (x[i] < x[j])    //Sort from large to small

                    {

                        int temp;

                        temp = x[i];

                        x[i] = x[j];

                        x[j] = temp;

                    }

                }

            }

        }

        static void Main(string[] args)

        {

            int[] huage = { 1, 5, 2, 9, 3, 7, 6,4,8,0};

            bubble_sort(huage);

            foreach (var a in huage)

            {

                Console.WriteLine(a );

            }

        }

2. Describe the implementation process of indexer in C # and whether it can only be indexed according to numbers

Indexer allows an object to be indexed like an array. When you define an indexer for a class, the class behaves like a virtual array. You can use the array access operator ([]) to access instances of this class.

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
        private string[] namelist = new string[size];
        static public int size = 10;
        public IndexedNames()
        {
            for (int i = 0; i < size; i++)
            {          
                namelist[i] = "N. A.";       
            }       
        }
        public string this[int index]
        {
            get{
                 string tmp;
                 if( index >= 0 && index <= size-1 ){
                     tmp = namelist[index];
                 }else{
                     tmp = "";
                 }
                 return (tmp);
             }
             set{
                 if( index >= 0 && index <= size-1 )
                 {
                     namelist[index] = value;
                 }
             }
        }
        static void Main(string[] args)
        {
            IndexedNames names = new IndexedNames();
            names[0] = "Zara";
            names[1] = "Riz";
            names[2] = "Nuha";
            names[3] = "Asif";
            names[4] = "Davinder";
            names[5] = "Sunil";
            names[6] = "Rubic";
            for ( int i = 0; i < IndexedNames.size; i++ )
            {             
                Console.WriteLine(names[i]);
            }
            Console.ReadKey();
        }
    }
} 

3.C# data type

In C#, variables are divided into the following types:

  • Value types
  • Reference types
  • Pointer types

1. Value type variables can be assigned directly to a value. They are from the class system Derived from ValueType +. Value types contain data directly. For example, int, char and float store numbers, letters and floating-point numbers respectively.

2. Reference types do not contain the actual data stored in variables, but they contain references to variables. In other words, they refer to a memory location. When using multiple variables, the reference type can point to a memory location. If the data of memory location is changed by one variable, other variables will automatically reflect the change of this value. The built-in reference types are object, dynamic and string.

3. Pointer type variables store another type of memory address. Pointers in C # have the same function as pointers in C or C + +.

4. What is packing and unpacking?

Boxing is the implicit conversion of a value type to a reference type object.

Unpacking is to convert a reference object into an arbitrary value object.

For example:

int i=0;

Syste.Object obj=i;

This process is packing! Is to pack the box!

For example:

int i=0;

System.Object obj=i;

int j=(int)obj;

In this process, the first two sentences are to pack , i , and the last sentence is to unpack , obj ,!

5. What are the common methods to call WebService?

1. Use WSDL Exe command line tool.

2. Use the Add Web Reference menu option in VS.NET

web service :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication1
{
    /// <summary>
    /// WebService1 Summary description of
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow ASP.NET AJAX Call this from script Web Service, please uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int add(int a, int b)
        {
            return a+b;
        }
    }
}

Call example:

class Program
    {
        static void Main(string[] args)
        {

            WebApplication1.WebService1 wb = new WebService1();
            int e = wb.add(4, 5);
            Console.WriteLine(e);
            Console.ReadLine();

        }
    }

 6. Please elaborate on the similarities and differences between class and structure in C#?

class can be instantiated and belongs to reference type. class can implement interfaces and single inheritance from other classes. It can also be used as a base type and is allocated on the memory heap.

struct is a value type and cannot be used as a base type, but it can implement an interface and is allocated on the memory stack.

The following procedure demonstrates the use of the structure:

using System;
     
struct Books
{
   public string title;
   public string author;
   public string subject;
   public int book_id;
};  

public class testStructure
{
   public static void Main(string[] args)
   {

      Books Book1;        /* Declare Book1 of type book */
      Books Book2;        /* Declare Book2 of type book */

      /* book 1 Elaborate */
      Book1.title = "C Programming";
      Book1.author = "Nuha Ali"; 
      Book1.subject = "C Programming Tutorial";
      Book1.book_id = 6495407;

      /* book 2 Elaborate */
      Book2.title = "Telecom Billing";
      Book2.author = "Zara Ali";
      Book2.subject =  "Telecom Billing Tutorial";
      Book2.book_id = 6495700;

      /* Print Book1 information */
      Console.WriteLine( "Book 1 title : {0}", Book1.title);
      Console.WriteLine("Book 1 author : {0}", Book1.author);
      Console.WriteLine("Book 1 subject : {0}", Book1.subject);
      Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);

      /* Print Book2 information */
      Console.WriteLine("Book 2 title : {0}", Book2.title);
      Console.WriteLine("Book 2 author : {0}", Book2.author);
      Console.WriteLine("Book 2 subject : {0}", Book2.subject);
      Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);       

      Console.ReadKey();

   }
}

 

Added by alwaysme on Tue, 18 Jan 2022 11:09:05 +0200