→ Modularity
Goal of modularity is to decompose the source code into small modules such that it should be possible to design, develop, test and maintain the individual modules independent of each other. It should be possible to change the implementation of one module without knowing or affecting the implementation of other modules.
Example of Modularity:
For example, a banking application deals with Current/Saving accounts, Fixed Deposits, Home Loans, and Personal Loans etc. Each of these functionalities should be implemented as independent modules to achieve greater flexibility.
Modularity has an added advantage when it comes to maintenance of the application.
First, you know exactly where to look for in case of a problem.
Secondly, after the issue is resolved, entire source code need not be recompiled and tested all over again. It’s only the updated module that needs to be recompiled and tested again.
→Instance
The instance is the actual object created at runtime. One can have an instance of a class or a particular object.
→State
The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that's defined in the object's class.
→Method
Method describes the object’s abilities. A Dog has the ability to bark. So bark () is one of the method of the Dog class.
→Message Passing
The process by which an object sends data to another object or asks the other object to invoke a method. Message passing corresponds to "method calling". This is also known as interfacing.
→Type Casting
Type casting is converting one data type to another.
decimal a=26;
int b= (int) a;
(Note: The value after the point (.) I'll be ignore, when converting decimal variable to int.)
Implicit Casting (Safe)
Automatically done by the compiler (smaller → larger type):
int num = 100;
float f = num; // Implicit casting (int to float)
Explicit Casting (Risky)
Manually done by the developer (larger → smaller type):
double d = 123.45;
int i = (int)d; // i = 123, fractional part lost
Using Convert class:
string str = "100";
int val = Convert.ToInt32(str); // Safer way to convert string to int
→Boxing Unboxing
Converting a value type (like int) to an object type (reference type).
int num = 123;
object obj = num; // Boxing
Extracting value type from a boxed object.
object obj = 123;
int num = (int)obj; // Unboxing
→Value Type
Reference and contents both are on same place (on stack). Use for small and fix types like int etc.
→Reference Type
Reference on stack but content are on heap (Dynamic Memory). Use for bigger variables. We can call it safe pointer. Class, string, array, object, interface are all of the examples of Reference Type.
→What is Stack and Heap?
Stack (Value Types)
Memory used for value types, method calls, local variables.
Fast and automatically managed.
Follows LIFO (Last-In-First-Out).
Heap (Reference Types)
Memory used for objects and reference types.
Garbage Collector (GC) manages heap memory.
Slower than stack (dynamic memory).
Example Flow:
int x = 10; // Stored in stack
Person p = new Person(); // Reference stored in stack, object in heap
p.Name = "John"; // Name is stored in heap
Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM .
Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer.
Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory. Element of the heap have no dependencies with each other and can always be accessed randomly at any time. You can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.
You can use the stack if you know exactly how much data you need to allocate before compile time and it is not too big. You can use heap if you don't know exactly how much data you will need at runtime or if you need to allocate a lot of data.
In a multi-threaded situation each thread will have its own completely independent stack but they will share the heap. Stack is thread specific and Heap is application specific. The stack is important to consider in exception handling and thread executions.
→ACII (American Standard Code for Information Interchange) & Unicode
ACII is fit for languages like English, Spanish, Japanese etc. But it not support many other languages like Arabic, Urdu etc. To overcome this problem now standard is Unicode based.
ASCII
Uses 7 bits to represent characters (0–127).
Represents basic English letters, digits, and symbols.
Limited character support.
Example:
char ch = 'A';
int asciiValue = (int)ch; // Output: 65
Character ASCII Code
'A' 65
'a' 97
'0' 48
Unicode (Universal Character Encoded System)
Supports all characters in all languages.
Uses UTF-8, UTF-16, UTF-32 encodings.
C# uses Unicode (UTF-16) for char and string.
Example:
char ch = 'ش'; // Urdu character
Console.WriteLine((int)ch); // Unicode code point
Encoding Description
UTF-8 Variable length, 1-4 bytes
UTF-16 Fixed (2 bytes), used in .NET
UTF-32 Fixed (4 bytes), rarely used
UTF stands for UCS (Unicode) Transformation Format
→Operators
A. Binary Operators: Binary operators are Equal (==), Not Equal (!=), Less Than (<), Greater Than (>) etc. Its often used.
B. Unary Operators: Contains only one operator like !(True)
C. Conditional: Operators/Ternary Operators (Loops)
→Notations
Camel Notation: The first letter of identifier is lowercase and the first letter of each subsequent concatenated word is capitalized like streetAddress etc. It is often use for Local, Private, Internal members.
Pascal Notation: The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized like StreetAddress etc. It is often use for Public and Protected members.
→Nullable for int
Nullable<int> a=null;
OR
int? a=null;
→Arrays
Array is a Type to store a similar type of data. Continued memory occupy by Array.
Examples:
Declaring Array in C#;
double[] salary = new double[10];
salary[0] = 7500.0;
double[] salary = { 340.0, 4523.69, 321.0};
int [] studentMarks = new int[5] { 49, 98, 92, 67, 75};
int [] studentMarks = new int[] { 99, 68, 97, 77, 95};
Copying of Array to an other array variable;
int[] score = studentMarks;
Accessing Array Elements;
double salary1 = salary[9];
→Structures
Structure is a type. It contains different data types. Like classes, structure is also a data type having members. Structures are not inherited and cannot inherited any other Type. For fixed and small piece of data we use structure. It is faster than the Class. Structure fields are not reference type. It is made on the stack so it is faster than the class. Structs are a value type and therefore inherit System.ValueType.
Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed. Structure can implement one or more interfaces. Structure members cannot be specified as abstract, virtual, or protected.
→Main Method in Console
In console the entry point is start from the Main() method. The compiler treat it as a entry point. It also treat it as a Main Keyword. No matter what the class name is, but to compile and the run the Console Application, we must declare the static Main() method (case sensitive).
→Primitive Data Types
Every variable has a type declared in the source code. There are two kinds of types: reference types and primitive types. Reference types are references to objects. Primitive types directly contain values. There are 8 primitive types:
• byte
• short
• int
• long
• char
• float
• double
• boolean
Primitive data types are the basic data types provided by C# and .NET. All primitive types are value types (stored in stack) and inherit from System.ValueType.
Non-Primitive (UnPrimitive) Data Types
These are complex types built using primitive types like string, object, class, array, delegate, interface.
The term "data type" and "primitive data type" are often used interchangeably. Primitive data types are predefined types of data, which are supported by the programming language. For example, integer, and character are all primitive data types. Programmers can use these data types when creating variables in their programs. For example, a programmer may create a variable called "age" and define it as a int data type. The variable will then store data as a int type values.
Non-primitive data types are not defined by the programming language, but are instead created by the programmer. They are sometimes called "reference variables," or "object references," since they reference a memory location, which stores the data. Non-primitive data types are simply called "objects" because they are created, rather than predefined. While an object may contain any type of data, the information referenced by the object may still be stored as a primitive data type.
In C#, string is a non-primitive data type.
Explanation:
-
string is an alias for the System.String class in .NET, which makes it a reference type.
-
All primitive types in C# are value types and include:
int, float, double, bool, char, byte, short, long, decimal, sbyte, ushort, uint, ulong.
Summary:
| Type | Primitive? | Value/Reference Type |
|---|
int | ✅ Yes | ✅ Value type |
string | ❌ No | ❌ Reference type |
So, even though string is commonly used and behaves in some "primitive-like" ways, it is not a primitive type. It's a non-primitive, reference type backed by the System.String class
string is built-in in C# but still not a primitive.
🔍 What does "primitive" mean?
In C#, primitive types are:
-
Value types
-
Directly mapped to low-level CLR (Common Language Runtime) types
-
Not objects, although they can be boxed into one
-
Examples: int, float, char, bool
📌 What is string then?
-
✅ It is built into the C# language — you can use string just like int.
-
✅ It has a keyword alias: string → System.String
-
❌ But it is a reference type, and an actual class in the .NET Base Class Library (BCL).
-
❌ It is not a primitive type, because it does not meet the criteria of being a value type.
💡 Summary
| Feature | int (e.g. primitive) | string |
|---|
| Built-in keyword | ✅ Yes (int) | ✅ Yes (string) |
| Underlying type | System.Int32 | System.String |
| Value or Reference type | ✅ Value type | ❌ Reference type |
| Primitive type | ✅ Yes | ❌ No |
| Class in BCL | ❌ No | ✅ Yes |
🔁 Analogy:
Even though C# language provides string as a keyword, it wraps around a .NET class, so it’s built-in but not primitive.
Let me know if you'd like a small code demo showing how string behaves like a reference type.
(Practice Makes a Man Perfect)
No comments:
Post a Comment