Official Study Materials for Computer Science
Explore structured advanced study materials through the Application of .NET Technology Unit 02 C Sharp Basics Notes Set 02. Tailored for learners, utilizing these Computer Science resources ensures thorough preparation and strengthens foundational knowledge before final evaluations.
Advanced Resources for Computer Science
Access the complete useful resource PDF for Computer Science below. Regular practice with these targeted academic notes builds familiarity with complex topics and helps secure higher marks in final school evaluations.
These notes continue C# with arrays, strings and the System.Collections classes such as ArrayList, Hashtable, SortedList, Stack, Queue and BitArray. They finish with delegates and events, indexers and their overloading, and predefined and custom attributes.
Array
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data. Instead of declaring individual variables, such as number0, number1, ..., and number99, declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Declaring Array
To declare an array in C#, you can use the following syntax:
datatype[] arrayName;
Where,
1. datatype is used to specify the type of elements to be stored in the array.
2. [ ] specifies the rank of the array. The rank specifies the size of the array.
3. arrayName specifies the name of the array.
For example,
double[] balance;
Initializing an Array
Declaring an array does not initialize the array in memory. When the array variable is initialized, you can assign values to the array. Array is a reference type, so you need to use the new keyword to create an instance of the array. For example,
double[] balance = new double[10];
Assigning Values to an Array
You can assign values to individual array elements, by using the index number, like:
double[] balance = new double[10]; balance[0] = 4500.0;
You can assign values to the array at the time of declaration, like:
double[] balance = { 2340.0, 4523.69, 3421.0 };You can also create and initialize an array, like:
int[] marks = new int[5] { 99, 98, 92, 97, 95 };When you create an array, the C# compiler implicitly initializes each array element to a default value depending on the array type. For example, for an int array all elements would be initialized to 0.
Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.
double salary = balance[9];
Multi-dimensional Arrays
C# allows multidimensional arrays. Multi-dimensional arrays are also called rectangular arrays. You can declare a 2-dimensional array of strings as: string[,] names; or a 3-dimensional array of int variables: int[,,] m;
The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional array is, in essence, a list of one-dimensional arrays. A 2-dimensional array can be thought of as a table, which will have x number of rows and y number of columns. For example, a 2-dimensional array can contain 3 rows and 4 columns.
Jagged Arrays
A jagged array is an array of arrays. You can declare a jagged array scores of int values as:
int[][] scores;
Passing Arrays to Functions
You can pass an array as a function argument in C#. The following example demonstrates this:
using System;
namespace ArrayApplication
{
class MyArray
{
double getAverage(int[] arr, int size)
{
int i;
double avg;
int sum = 0;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
avg = (double)sum / size;
return avg;
}
static void Main(string[] args)
{
MyArray app = new MyArray();
int[] balance = new int[] { 1000, 2, 3, 17, 50 };
double avg;
avg = app.getAverage(balance, 5);
Console.WriteLine("Average value is: {0} ", avg);
Console.ReadKey();
}
}
}When the above code is compiled and executed, it produces the following result:
Average value is: 214.4
Array Class
The Array class is the base class for all the arrays in C#. It is defined in the System namespace. The Array class provides various properties and methods to work with arrays.
| Property | Description |
|---|---|
| IsFixedSize | Gets a value indicating whether the Array has a fixed size. |
| IsReadOnly | Gets a value indicating whether the Array is read-only. |
| Length | Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array. |
| LongLength | Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array. |
| Rank | Gets the rank (number of dimensions) of the Array. |
Strings
A string can be thought of as an array of characters: [H][e][l][l][o]. In C#, you can use strings as an array of characters, however, the more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System.String class.
Creating a String Object: you can create a string object using one of the following methods:
1. By assigning a string literal to a String variable.
2. By using a String class constructor.
3. By using the string concatenation operator (+).
4. By retrieving a property or calling a method that returns a string.
5. By calling a formatting method to convert a value or object to its string representation.
Example for creating a String Object:
using System;
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
//from string literal and string concatenation
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//by using string constructor
char[] letters = { 'H', 'e', 'l', 'l', 'o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
}
}
}The String class has the following two properties:
1. Chars gets the Char object at a specified position in the current String object.
2. Length gets the number of characters in the current String object.
System Collections
Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index. These classes create collections of objects of the Object class, which is the base class for all data types in C#. Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces.
ArrayList
It represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However, unlike an array, you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.
The following table lists some of the commonly used properties of the ArrayList class:
| Property | Description |
|---|---|
| Capacity | Gets or sets the number of elements that the ArrayList can contain. |
| Count | Gets the number of elements actually contained in the ArrayList. |
| IsFixedSize | Gets a value indicating whether the ArrayList has a fixed size. |
| IsReadOnly | Gets a value indicating whether the ArrayList is read-only. |
| Item | Gets or sets the element at the specified index. |
The following table lists some of the commonly used methods of the ArrayList class:
| S.N | Method Name and Purpose |
|---|---|
| 1 | public virtual int Add(object value); Adds an object to the end of the ArrayList. |
| 2 | public virtual void AddRange(ICollection c); Adds the elements of an ICollection to the end of the ArrayList. |
| 3 | public virtual void Clear(); Removes all elements from the ArrayList. |
| 4 | public virtual bool Contains(object item); Determines whether an element is in the ArrayList. |
| 5 | public virtual ArrayList GetRange(int index, int count); Returns an ArrayList which represents a subset of the elements in the source ArrayList. |
| 6 | public virtual int IndexOf(object); Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it. |
Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.
The following table lists some of the commonly used properties of the Hashtable class:
| Property | Description |
|---|---|
| Count | Gets the number of key-and-value pairs contained in the Hashtable. |
| IsFixedSize | Gets a value indicating whether the Hashtable has a fixed size. |
| IsReadOnly | Gets a value indicating whether the Hashtable is read-only. |
| Item | Gets or sets the value associated with the specified key. |
| Keys | Gets an ICollection containing the keys in the Hashtable. |
| Values | Gets an ICollection containing the values in the Hashtable. |
A hash table is used when you need to access elements by using a key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
SortedList
The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.
The following table lists some of the commonly used properties of the SortedList class:
| Property | Description |
|---|---|
| Capacity | Gets or sets the capacity of the SortedList. |
| Count | Gets the number of elements contained in the SortedList. |
| IsFixedSize | Gets a value indicating whether the SortedList has a fixed size. |
| IsReadOnly | Gets a value indicating whether the SortedList is read-only. |
| Item | Gets and sets the value associated with a specific key in the SortedList. |
| Keys | Gets the keys in the SortedList. |
| Values | Gets the values in the SortedList. |
Stack
It represents a last-in, first-out collection of objects. It is used when you need last-in, first-out access of items. When you add an item to the list, it is called pushing the item. When you remove it, it is called popping the item.
The following table lists some of the commonly used properties of the Stack class:
| Property | Description |
|---|---|
| Count | Gets the number of elements contained in the Stack. |
Queue
It represents a first-in, first-out collection of objects. It is used when you need first-in, first-out access of items. When you add an item to the list, it is called enqueue. When you remove an item, it is called dequeue.
The following table lists some of the commonly used properties of the Queue class:
| Property | Description |
|---|---|
| Count | Gets the number of elements contained in the Queue. |
BitArray
The BitArray class manages a compact array of bit values, where true indicates that the bit is on (1) and false indicates the bit is off (0). It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.
Delegates and Events
C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.
Working with delegates involves: 1. Declaring Delegates.
2. Instantiating Delegates.
3. Multicasting of a Delegate.
4. Use of Delegate.
Declaring Delegates
Syntax for delegate declaration is:
delegate <return type> <delegate-name> <parameter list>
Example:
public delegate int MyDelegate (string s);
Delegate declaration determines the methods that can be referenced by the delegate. A delegate can refer to a method which has the same signature as that of the delegate.
Instantiating Delegates
Once a delegate type has been declared, a delegate object must be created with the new keyword and be associated with a particular method. When creating a delegate, the argument passed to the new expression is written like a method call, but without the arguments to the method. For example:
public delegate void printString(string s); ... printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile);
Following example demonstrates declaration, instantiation, and use of a delegate that can be used to reference methods that take an integer parameter and return an integer value.
using System;
delegate int NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
static void Main(string[] args)
{
//create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
//calling the methods using the delegate objects
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}When the above code is compiled and executed, it produces the following result:
Value of Num: 35 Value of Num: 175
Multicasting of a Delegate
The following program demonstrates multicasting of a delegate:
using System;
delegate int NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
static void Main(string[] args)
{
//create delegate instances
NumberChanger nc;
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
nc = nc1;
nc += nc2;
//calling multicast
nc(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}When the above code is compiled and executed, it produces the following result:
Value of Num: 75
Delegate objects can be composed using the "+" operator. A composed delegate calls the two delegates it was composed from. Only delegates of the same type can be composed. The "-" operator can be used to remove a component delegate from a composed delegate. Using this useful property of delegates you can create an invocation list of methods that will be called when a delegate is invoked. This is called multicasting of a delegate.
Use of Delegate
The following example demonstrates the use of a delegate. The delegate printString can be used to reference methods that take a string as input and return nothing.
using System;
using System.IO;
namespace DelegateAppl
{
class PrintString
{
static FileStream fs;
static StreamWriter sw;
// delegate declaration
public delegate void printString(string s);
// this method prints to the console
public static void WriteToScreen(string str)
{
Console.WriteLine("The String is: {0}", str);
}
//this method prints to a file
public static void WriteToFile(string s)
{
fs = new FileStream("c:\\message.txt",
FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
// this method takes the delegate as parameter and uses it to
// call the methods as required
public static void sendString(printString ps)
{
ps("Hello World");
}
static void Main(string[] args)
{
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);
sendString(ps1);
sendString(ps2);
Console.ReadKey();
}
}
}When the above code is compiled and executed, it produces the following result:
The String is: Hello World
Events
An event is a delegate with special features in the areas of type membership, limitations on invocation, and assignment. By having events as first-class type members, along with fields, methods, and properties, C# becomes a more component-oriented language. This means that other objects can listen for changes in your object and receive notifications via event invocations.
Using Delegates with Events
The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the event. This is called the publisher class. Some other class that accepts this event is called the subscriber class. Events use the publisher-subscriber model.
A publisher is an object that contains the definition of the event and the delegate. The event-delegate association is also defined in this object. A publisher class object invokes the event and it is notified to other objects.
Subscriber: a subscriber is an object that accepts the event and provides an event handler. The delegate in the publisher class invokes the method (event handler) of the subscriber class.
using System;
namespace SimpleEvent
{
public class EventTest
{
private int value;
public delegate void NumManipulationHandler();
public event NumManipulationHandler ChangeNum;
protected virtual void OnNumChanged()
{
if (ChangeNum != null)
{
ChangeNum();
}
else
{
Console.WriteLine("Event fired!");
}
}
public EventTest(int n)
{
SetValue(n);
}
public void SetValue(int n)
{
if (value != n)
{
value = n;
OnNumChanged();
}
}
}
public class MainClass
{
public static void Main()
{
EventTest e = new EventTest(5);
e.SetValue(7);
e.SetValue(11);
Console.ReadKey();
}
}
}When the above code is compiled and executed, it produces the following result:
Event Fired! Event Fired! Event Fired!
Indexers
Declaration of the behavior of an indexer is to some extent similar to a property. Like properties, you use get and set accessors for defining an indexer. However, properties return or set a specific data member, whereas indexers return or set a particular value from the object instance. In other words, it breaks the instance data into smaller parts and indexes each part, gets or sets each part.
An indexer allows an object to be indexed like an array. Define an indexer for a class, and this class behaves like a virtual array. You can then access the instance of this class using the array access operator ([ ]).
Use of Indexers
Defining a property involves providing a property name. Indexers are not defined with names, but with the this keyword, which refers to the object instance. The syntax is:
element-type this[int index]
{
// The get accessor.
get
{
// return the value specified by index
}
// The set accessor.
set
{
// set the value specified by index
}
}The following example demonstrates the concept:
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";
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}Overloaded Indexers
Indexers can be overloaded. Indexers can also be declared with multiple parameters and each parameter may be a different type. It is not necessary that the indexes have to be integers. C# allows indexes to be of other types, for example, a string.
Attributes
An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc., in your program. You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for. Attributes are used for adding metadata, such as compiler instructions and other information such as comments, description, methods and classes to a program.
The .NET Framework provides two types of attributes: the pre-defined attributes and custom built attributes. Under different classifications the attributes are specified and some predefined attributes are also available in C#: AttributeUsage, Conditional and Obsolete.
To create a custom attribute a few steps have to be followed: declaring a custom attribute, constructing the custom attribute, applying the custom attribute on a target program element, and accessing attributes through reflection.
Syntax for specifying an attribute is as follows:
[attribute(positional_parameters, name_parameter = value, ...)] Element
The name of the attribute and its values are specified within the square brackets, before the element to which the attribute is applied. Positional parameters specify the essential information and the name parameters specify the optional information.
AttributeUsage
The pre-defined attribute AttributeUsage describes how a custom attribute class can be used. It specifies the types of items to which the attribute can be applied. For example, a custom attribute BugFix to be assigned to a class and its members:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class DeBugInfo : System.Attribute
Conditional
This predefined attribute marks a conditional method whose execution depends on a specified preprocessing identifier. It causes conditional compilation of method calls, depending on the specified value such as Debug or Trace. For example, it displays the values of the variables while debugging a code. Syntax for specifying this attribute is as follows:
[Conditional(conditionalSymbol)]
Obsolete
This predefined attribute marks a program entity that should not be used. It enables you to inform the compiler to discard a particular target element. For example, when a new method is being used in a class, but you still want to retain the old method in the class, you may mark it as obsolete by displaying a message that the new method should be used, instead of the old method. Syntax for specifying this attribute is as follows:
[Obsolete(message)] [Obsolete(message, iserror)]
Creating Custom Attributes
The .NET Framework allows creation of custom attributes that can be used to store declarative information and can be retrieved at run-time. This information can be related to any target element depending upon the design criteria and application need. Creating and using custom attributes involve four steps:
1. Declaring a custom attribute.
2. Constructing the custom attribute.
3. Applying the custom attribute on a target program element.
4. Accessing attributes through reflection.
The last step involves writing a simple program to read through the metadata to find various notations. Metadata is data about data, or information used for describing other data. This program should use reflection for accessing attributes at runtime.
Free study material for Computer Science
Free CBSE Study Guides: Computer Science .NET Technology
Comprehensive Study Resources for .NET Technology
Explore essential learning tools for Computer Science .NET Technology. This curated collection features in-depth notes and targeted practice questions built around the active 2026 curriculum to streamline your daily revision.
Understanding Marking Schemes
Designed around the official curriculum, these study guides guarantee standard compliance. Reviewing step-by-step solutions clarifies complex sub-topics within .NET Technology and demystifies standard marking schemes for Computer Science evaluations.
Complete Revision for Computer Science
Maximize your grade potential by utilizing Computer Science practice papers in tandem with these structured notes. All printable assignments and interactive mock tests on our platform are updated for the 2026 session and available free of charge.
FAQs
The latest 2026-27 advanced study resources for Computer Science are available for free on StudiesToday.com which includes NCERT Exemplars, high-order thinking skills (HOTS) questions, and deep-dive concept summaries.
Our exhaustive Computer Science package includes chapter wise revision notes, solved practice sheets, important formulas and Concept Maps to help in better understanding of all topics.
Yes. For , our resources have been developed to help you get better marks in school exams and also build fundamental strength needed for entrance tests including Competency Based learning.
in , students should use Active Recall method, read the concept summary, then solve the Important Questions section without looking at the answers and then check your answers.
All Computer Science study materials are provided in mobile-friendly PDF. You can download and save them on your device.
Yes, our team has ensured that all Computer Science materials for are strictly aligned with the National Education Policy (NEP) 2020 and the latest 2026-27 syllabus.