Download INdexers, properties, linq and more Assignments Programming Languages in PDF only on Docsity!
C# Notes: Indexers, Properties, and LINQ
1. PROPERTIES
What is a Property?
A property is a member of a class that provides a flexible mechanism to read, write, or compute
the value of a private field.
Properties provide encapsulation by controlling access to data.
Why Use Properties?
- Protect data from direct access.
- Perform validation before assigning values.
- Read-only or write-only access.
- Better object-oriented design.
Basic Property Syntax
class Student { private string name; public string Name { get { return name; } set { name = value; } } }
Usage:
Student s = new Student(); s.Name = "Ali"; Console.WriteLine(s.Name);
get Accessor
Used to retrieve value.
public string Name { get { return name; } } set Accessor
Used to assign value.
public string Name { set { name = value; } } The value Keyword
Inside a setter, value contains the assigned value.
set { name = value; }
Example:
s.Name = "Ahmed";
Here:
value = "Ahmed" Read-Only Property
Can only be read.
public string Name { get { return name; } }
OR
public string Name { get; }
if(value >= 18) age = value; } }
Expression-Bodied Property
public string FullName => firstName + " " + lastName;
Static Property
Belongs to class.
public static int Count { get; set; }
Access:
Student.Count
Difference: Field vs Property
Field Property
Stores data Controls access
Usually private Usually public
No validation Validation possible
Direct access Through get/set
2. INDEXERS
What is an Indexer?
An indexer allows an object to be indexed like an array.
Without indexer:
student.GetName(0);
With indexer:
student[0]; Syntax class Demo { private string[] names = new string[5]; public string this[int index] { get { return names[index]; } set { names[index] = value; } } }
Usage:
Demo d = new Demo(); d[0] = "Ali"; Console.WriteLine(d[0]);
Output:
Ali How Indexers Work
Compiler translates:
d[0]
into:
d.this[0] Multiple Indexers class Student
public string this[int row, int col] { get { return data[row, col]; } set { data[row, col] = value; } }
Usage:
obj[1,2] = "A";
Difference: Property vs Indexer
Property Indexer
Uses name Uses this
Accessed with dot Accessed with []
Represents single value Represents collection
Example: obj.Name Example: obj[0]
3. LINQ (Language Integrated Query)
What is LINQ?
LINQ is a feature in C# that allows querying data using SQL-like syntax.
LINQ can query:
- Arrays
- Lists
- Collections
- DataTables
- XML
- Databases
Namespace
using System.Linq; Benefits of LINQ
- Less code
- Easy to read
- Strongly typed
- Compile-time checking
- SQL-like syntax LINQ Query Syntax var result = from n in numbers where n > 10 select n; Example int[] numbers = { 10,20,30,40, }; var result = from n in numbers where n > 25 select n; foreach(var x in result) { Console.WriteLine(x); }
Output:
Method Syntax var result = numbers.Where(n => n > 25);
Descending sort.
var result = numbers.OrderByDescending(x => x); ThenBy()
Second sorting criteria.
students.OrderBy(s => s.City) .ThenBy(s => s.Name); First()
Returns first element.
numbers.First();
Output:
FirstOrDefault()
Returns first or default value.
numbers.FirstOrDefault();
If empty:
Last() numbers.Last(); LastOrDefault() numbers.LastOrDefault();
Single()
Exactly one element.
numbers.Single(x => x == 20);
Throws exception if:
- None found
- More than one found SingleOrDefault()
Returns default if none exists.
Count() numbers.Count(); Sum() numbers.Sum(); Average() numbers.Average(); Max() numbers.Max(); Min() numbers.Min();
Reverse() numbers.Reverse(); GroupBy()
Groups data.
var result = students.GroupBy(s => s.City); Join()
Equivalent to SQL Join.
var result = students.Join( departments, s => s.DeptId, d => d.Id, (s,d) => new { s.Name, d.DeptName }); ToList()
Convert to List.
var list = numbers.ToList(); ToArray() var arr = numbers.ToArray(); Anonymous Types
var student = new { Id = 1, Name = "Ali" };
Used frequently with LINQ.
Lambda Expressions
LINQ commonly uses lambda expressions.
x => x > 10
Equivalent to:
bool Check(int x) { return x > 10; }
Frequently Asked Exam Questions
What is LINQ?
Language Integrated Query used to query collections and databases using SQL-like syntax.
What is an Indexer?
A special class member that allows objects to be accessed like arrays using [].
What is a Property?
A class member that provides controlled access to fields using get and set.
Difference between Query Syntax and Method Syntax?
Query Syntax Method Syntax
SQL-like Uses methods
Easier to read More powerful
from where select Where() Select()