Programming Language Paradigms
• Object-Oriented Programming (OOP): OOP organizes code into objects, which are
instances of classes. It focuses on the concept of objects that contain data and code. OOP
features encapsulation, inheritance, and polymorphism. It's useful for modeling real-world
entities and creating modular, reusable code.
• Imperative Programming: This paradigm uses statements that change a program's state. It
focuses on describing how a program operates, step-by-step. Imperative programming is based
on the idea of giving the computer a sequence of tasks to perform, changing the program state
with each step.
• Functional Programming: Functional programming treats computation as the evaluation of
mathematical functions. It emphasizes immutable data and pure functions (functions without
side effects). This paradigm avoids changing state and mutable data, making it easier to
understand and predict program behavior.
• Procedural Programming: Procedural programming is based on the concept of procedure
calls. It divides the program into procedures (also known as routines or functions), which
contain a series of computational steps to be carried out. It's a type of imperative programming
that focuses on breaking down a program into a collection of procedures.
• Event-Driven Programming: In event-driven programming, the flow of the program is
determined by events such as user actions, sensor outputs, or messages from other programs.
This paradigm is widely used in graphical user interfaces and real-time systems where the
program needs to react to external inputs.
• Generic Programming: Generic programming focuses on writing code that can work with
any data type. It allows writing functions or classes that can operate on different data types
without being rewritten for each one. This paradigm enhances code reusability and type safety.
• Declarative Programming: Declarative programming focuses on what the program should
accomplish without explicitly specifying how to do it. It describes the desired result without
listing the steps to achieve it. SQL (for database queries) and HTML (for web page structure)
are examples of declarative languages.
• And more…
Multi Paradigm Programming Languages:
Understanding and Working with Data Types
1. Introduction to C# Data Types
• Primitive, Value, Reference and Complex Data Types
o Difference between value types (e.g., int, bool, char) and reference types
(e.g., object, string, arrays).
• C# Type System
Overview of C#'s static type system and the concept of strong typing.
• Common Data Types
o Numeric types: int, double, decimal, float, etc.
o Text types: char, string.
o Boolean type: bool.
EXAMPLES: 1. Introduction to C# Data Types
using System.Data.Common;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
namespace W01
{
class Program
{
static void Main(string[] args)
{
//Primitive Types -----------------------------------------:
//has only a single value
int i1 = 1;
double d1 = 1.0;
bool b1 = false;
char c1 = 'a';
//Complex Types ------------------------------------------:
//has more than one values or types
WekDays monday = WekDays.Monday;
int[] arr1 = new[] { 1, 2, 4 };
MyStruct myStruct1 = new MyStruct();
MyClass myClass1 = new MyClass();
List<MyStruct> list = new List<MyStruct>();
var people2 = new
{
Person = "Ahmet",
Age = 12,
Ob1 = new MyClass()
};
dynamic dy4 = new { x = 1, y = 2 };
string name1 = "";
// Value Types -------------------------------------------:
//cannot be null! Stored in Stack Section in Ram
int i2 = 1;
double d2 = 1.0;
bool b2 = false;
char c2 = 'a';
WekDays monday2 = WekDays.Monday;
MyStruct myStruct2 = new MyStruct();
// Reference Types:
int[] arr2 = new[] { 1, 2, 4 };
MyClass myClass2 = new MyClass();
List<MyStruct> list2 = new List<MyStruct>();
var people1 = new
{
Person = "Ahmet",
Age = 12,
Ob1 = new MyClass()
};
dynamic dy3 = new {x=1,y=2};
string name2 = "ad";
// Nullable Types:
int? i3 = null;
double? d3 = null;
bool? b3 = null;
char? c3 = null;
int[] arr3 = null;
MyStruct? myStruct4 = null;
MyClass myClass3 = null;
List<MyStruct> list3 = null;
var people3 = new
{
Person = "Ahmet",
Age = 12,
Ob1 = new MyClass()
};
dynamic dy2 = null;
string name3 = null;
// Anonymous Types:
var people = new
{
Person = "Ahmet",
Age = 12,
Ob1 = new MyClass()
};
// Dynamic types
dynamic dy1 = people;
Console.WriteLine(dy1.Person);
//dy1 = 23;
Console.WriteLine(dy1.Person);
// dy1 = "Hi";
//Records: //immutables//ref and complex types
Person person = null;//records
Person person1 = new Person("A", 2);
Person person2 = new Person("A", 2);
Console.WriteLine("Records are eq: " + (person1==person2)); //immutable objects so,
comparision made my values
MyClass my1 = new MyClass() {Value1 = 1,Value2 = "Aydin"};
MyClass my2 = new MyClass() {Value1 = 1, Value2 = "Aydin"};
Console.WriteLine("Object are eq: " + (my1 == my2));// mutable objects, compared by address
record Person(string name, int age); //reference type record, complex, nullable
record struct Person2(string name, int age); //value type record, not nullable, complex
//Immutable Types as Default, comparision made my their values
//*Records
//*Strings
}
//complex
enum WekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
struct MyStruct
{
public int Value1 { get; set; }
public string Value2 { get; set; }
}
class MyClass
{
public int Value1 { get; set; }
public string Value2 { get; set; }
}
}
2. Value Types vs. Reference Types
• Stack vs. Heap Memory Allocation
How value types and reference types are stored in memory.
• Boxing and Unboxing
o Converting a value type to a reference type (boxing) and vice versa
(unboxing).
• Mutable vs. Immutable Types
Understanding immutability with examples like string and DateTime.
3. Nullable Types
• Introduction to Nullable Types
o Why nullable types are useful (int?, bool?).
• Working with Nullable Types
Using Nullable<T> and the ?. and ?? operators for null-safe programming.
• Null-Coalescing Operator
Practical use of the null-coalescing operator to handle default values.
class Program2
{
static void Main(string[] args)
{
//nullable types
int? n1 = null;
Student student=null;
// student.Name = student?.Name ?? "Ahmet"; //error
Console.WriteLine(student?.Name);
Student student1 = new Student();
student1.Name = student1?.Name ?? "Ahmet";
Console.WriteLine(student1.Name?.Length);
class Student
{
public int? Id { get; set; }
public string? Name { get; set; }
}
4. Enumerations and Constants
• Defining and Using Enums
o Creating enumerations to represent a fixed set of related constants.
• Enums with Flags
o Using Flags attribute to represent multiple values in enums.
• Working with Constants (const vs readonly)
o Difference between const and readonly fields.
namespace W01
{
enum WeekDays //aut0 enumerated
{
Monday,
Sunday,
}
enum Gender //manuel enumerated
{
M = 15,
F = 19
}
class Program2
{
private readonly int a;
public Program2()
{
a = 4;//only can be changed during class creation or constructor.
}
static void Main(string[] args)
{
}
}
5. Strings and String Manipulation
• Immutable Nature of Strings
o Understanding how strings are immutable and the implications for
performance.
• StringBuilder for Efficient String Manipulation
o Using StringBuilder for performance when working with large or frequently
changing strings.
• Common String Operations
Methods like Substring(), IndexOf(), Replace(), Split(), and more.
class Program2
{
static void Main(string[] args)
{
string name = "Ahmet"; //Immutable / Low performance
name += " Seçer";
StringBuilder sb = new StringBuilder(); //Mutable / high performance
sb.Append("Ahmet");
sb.Append("Seçer");
//string ops.
Console.WriteLine(name.ToUpper());
Console.WriteLine(name.Length);
//:
//:
6. Collections and Arrays
• Arrays and Their Properties
o Single-dimensional and multi-dimensional arrays (int[], int[,]).
• Lists and Dictionaries
o Introduction to List<T> and Dictionary<TKey, TValue> for more dynamic
collections.
• Other Collection Types
o HashSet<T>, Queue<T>, Stack<T>, and when to use them.
7. Structs vs. Classes
• When to Use Structs vs. Classes
o Key differences between structs (value types) and classes (reference types).
• Performance Considerations
o Discussing when to use structs for performance reasons (small, lightweight
objects).
8. Tuples and Anonymous Types
• Using Tuples for Lightweight Data Grouping
o Introduction to tuples ((int, string)) and when to use them.
• Named Tuples
o Enhancing readability using named tuples.
• Anonymous Types
o Creating anonymous types for grouping data without defining a full class.
class Program2
{
static void Main(string[] args)
{
//tuples are mutable, complex types, value types
(string name, int age) student = ("Aydın", 35);
student.age = 34;
//student = null;
Console.WriteLine(student.name);
Console.WriteLine(student.age);
//anonymous types are immutable, complex, reference types
var course = new { Name = "Aydın", Age = 36 };
//course.Name = "Add";
Console.WriteLine(course.Name);
course = null; //nullable
}
}
9. Type Inference with var and Dynamic Types
• Type Inference with var
o How and when to use var for type inference in local variables.
• Dynamic Types in C#
o Introduction to the dynamic type and when it is appropriate to use.
• Pros and Cons of dynamic
o Understanding the trade-offs of using dynamic typing in a statically typed
language.
10. Type Conversion and Casting
• Implicit vs. Explicit Type Conversions
o Understanding implicit conversions and when explicit casting is necessary.
• Convert Class
o Using the Convert class to safely change data types.
• Working with as and is Operators
o Type checking and safe type casting using as and is.
class Program2
{
static void Main(string[] args)
{
//implicit type conversion
double d1 = 135163.5;
int a = 15;
d1 = a; //implicit conversion;
//explicit conversion
a = (int)d1;//cast operation explicit type conversion
d1 = Convert.ToDouble(a);//convert class
//a = d1 as int; as can be used only ref.
object name = "Aydın"; //boxing
object age = 123;
string strName = (string)name; //un boxing/ /expensive op
strName = name as string; // unboxing similar to the cast
Console.WriteLine(d1 is double); //check type operatos (is)
Console.WriteLine(name is string);
Console.WriteLine(age is double);
}
}
11. Custom Data Types
• Creating Custom Structs and Classes
o How to define custom data types that fit specific use cases.
• Using Custom Types with Collections
o Storing and managing custom types in collections like List<T>.
12. Project: Implementing a Simple Data Model
• Goal:
Create a simple data model using various data types (enums, structs, collections).
• Requirements:
o Use an enum to define a set of related constants (e.g., order status).
o Use structs for lightweight objects and classes for more complex data types.
o Store and manipulate the data using arrays and collections.
• Output:
Display the data model, performing basic operations like sorting, filtering, and
searching.