*
Previous C# Question Answers-71 - 80 C# Question Answers-91 - 100 Next

C# (C-Sharp) Question Answers - 81-90

81. What is ICollection?

The ICollection interface inherits from IEnumerable and defines size, enumerators, and synchronization methods for all non-generic collections. It provides a Count property and supports traversal with foreach.

82. What is IEnumerable?

IEnumerable allows enumeration of non-generic collections using foreach. It can move forward only and is read-only.

using System;
using System.Collections.Generic;
using System.Linq;

List<string> List = new List<string>() { "Basava", "Guru", "Sharan", "Shiva", "Allama" };
IEnumerable<string> names = from n in List where n.StartsWith("S") select n;
foreach (string name in names) { Console.WriteLine(name); }

// Output:
// Sharan
// Shiva

83. What is IList?

IList inherits from ICollection and IEnumerable. It represents a non-generic collection that can be accessed by index. Types of IList implementations:

  • Read-only: cannot be modified
  • Fixed-size: cannot add/remove, but can modify existing elements
  • Variable-size: allows add, remove, and modification

84. What is indexer property in C#?

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

// Example of indexer
class SampleCollection<T>
{
    // Declare an array to store the data elements.
    private T[] arr = new T[100];

    // Define the indexer, which will allow client code
    // to use [] notation on the class instance itself.
    // (See line 2 of code in Main below.)
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}
// This class shows how client code uses the indexer.
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>                ();

        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}

// Output:
// Hello, World.

85. What is Inheritance?

Inheritance specifies hierarchical relationships between types. A derived class inherits data and function members from a base class.

86. What is IQueryable?

IQueryable inherits from IEnumerable and allows queries against a data source with deferred execution.

MyDataContext dc = new MyDataContext();

IQueryable<Emp> list = dc.Employees.Where(n => n.Name.StartsWith("S"));
list = list.Take<Emp > (10);


87. What is mark-and-sweep algorithm?

Objects have a flag for GC. The algorithm works in two stages:

  • Mark: Traverse root set, mark reachable objects.
  • Sweep: Free memory of unmarked objects. Clear flags of marked objects.

This method has several disadvantages, the most notable being that the entire system must be suspended during collection; no mutation of the working set can be allowed. This will cause programs to 'freeze' periodically (and generally unpredictably), making real-time and time-critical applications impossible. In addition, the entire working memory must be examined, much of it twice, potentially causing problems in paged memory systems.

88. What is Marshalling?

Marshalling governs how data is passed between managed and unmanaged memory at runtime.

89. What is non-deterministic finalization?

In .net, we do not know exactly when the destructor is executed. It is called only during the garbage collection process and we do not know when it actually happens. This state is called non-deterministic destructors or InDeterministic destructors.

90. Output of the below code with goto label?

public class Exercise
{
    public static int Main()
    {
        for (var Stories = 1; Stories <= 14; Stories++)
        {
            if (Stories == 4)
                goto CountUpTo3;
            Console.WriteLine("Story {0}", Stories);
        }
    // label
    CountUpTo3:
        Console.WriteLine("Our homes have only up to 3 levels\n");

        return 0;
    }
}

Output:

Story 1
Story 2
Story 3
Our homes have only up to 3 levels
Back to Index
Previous C# Question Answers-71 - 80 C# Question Answers-91 - 100 Next
*
*