| C# Question Answers-71 - 80 | C# Question Answers-91 - 100 | |
C# (C-Sharp) Question Answers - 81-90 |
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.
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
IList inherits from ICollection and IEnumerable. It represents a non-generic collection that can be accessed by index.
Types of IList implementations:
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.
Inheritance specifies hierarchical relationships between types. A derived class inherits data and function members from a base class.
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);
Objects have a flag for GC. The algorithm works in two stages:
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.
Marshalling governs how data is passed between managed and unmanaged memory at runtime.
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.
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
| C# Question Answers-71 - 80 | C# Question Answers-91 - 100 | |