Posts

Showing posts from February, 2024

TypesOfCollections

 1.Generic collections/System.Collections.Generic 2.Non-Generic collections/System.Collections  3.Concurrent collections/System.Collections.Concurrent Generic collections/System.Collections.Generic: List Stack Queue LinkedList HashSet SortedSet Dictionary SortedDictionary SortedList Non-Generic collections/System.Collections: ArrayList Stack Queue Hashtable Concurrent collections/System.Collections.Concurrent: BlockingCollection ConcurrentBag ConcurrentStack ConcurrentQueue ConcurrentDictionary Partitioner Partitioner OrderablePartitioner

CallByValue&Reference

 1. Call by Value : Call by value is a method of passing arguments to a function where the actual value of the argument is passed to the function. In call by value, the function receives a copy of the argument's value, not the original variable. Changes made to the parameter inside the function do not affect the original value outside the function. Primitive data types like integers, floats, and characters are typically passed by value. Example in C#: void Increment(int x) { x++; // Increment the copy of x } int num = 10; Increment(num); // Call by value Console.WriteLine(num); // Output: 10 (original value unchanged) 2.Call by Reference : Call by reference is a method of passing arguments to a function where the reference (memory address) of the variable is passed to the function. In call by reference, the function receives a reference to the original variable, allowing it to modify the original value. Changes made to the parameter inside the function affect the original valu...

Types of Memories

  In C#, when we talk about "memory," we usually refer to the types of data storage or memory management available within the .NET runtime environment. Here are some common types of memory in C#: Stack Memory : Stack memory is used to store value types and references to objects. It operates in a last-in, first-out (LIFO) manner, and memory allocation and deallocation are handled automatically by the runtime. Local variables, method parameters, and return addresses are typically stored on the stack. Heap Memory : Heap memory is used to store objects and reference types. Memory allocation for objects on the heap is managed by the garbage collector, which automatically frees up memory that is no longer in use. Objects on the heap can be accessed through references stored on the stack or in other objects. Garbage-Collected Memory : In C#, memory management is handled by a garbage collector, which automatically deallocates memory that is no longer needed by the program. The garbag...

Collections

  Arrays: Fixed-size collections of elements of the same type. ArrayList:Any datatype can be inserted List<T>: Dynamic arrays that automatically resize as needed. Dictionary<TKey, TValue>: Collection of key-value pairs. HashSet<T>: Collection of unique elements. Queue<T>: First-in-first-out (FIFO) collection. Stack<T>: Last-in-first-out (LIFO) collection. LinkedList<T>: Doubly linked list. SortedDictionary<TKey, TValue>: Sorted collection of key-value pairs. SortedSet<T>: Sorted collection of unique elements. Hashtable: Non-generic collection of key-value pairs . SortedDictionary<TKey, TValue>: Sorted collection of key-value pairs. SortedSet<T>: Sorted collection of unique elements. BitArray: Collection of bits that can be individually set or cleared. ConcurrentBag<T>: Thread-safe collection for unordered bags of objects. ConcurrentDictionary<TKey, TValue>: Thread-safe collection of key-value pairs. ConcurrentQ...

Conditional statements:

 In C#, conditional statements are used to control the flow of execution based on certain conditions. Types: 1.if 2.if-else 3.else if 4.switch 1.if statement: The if statement is used to execute a block of code if a specified condition is true. *Single Statement if (condition) {     // Code to be executed if the condition is true } 2.if-else statement: The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false. *Two Statements if (condition) {     // Code to be executed if the condition is true } else {     // Code to be executed if the condition is false } 3.else if statement: The else if statement is used when you have multiple conditions to check. *Multiple Statements if (condition1) {     // Code to be executed if condition1 is true } else if (condition2) {     // Code to be executed if condition2 is true } else {     // Code to be executed if none...

Constructor

 In C#, a constructor is a special method in a class that is automatically called when an object of that class is created. It can be used to set initial values for fields. Types of Constructor 1.Default Constructor 2.Parameterized Constructor 3.Copy Constructor 4.Private Constructor 5.Static Constructor Definetions: 1.Default Constructor: If a class doesn't have any explicitly defined constructors, C# provides a default constructor with no parameters. 2.Parameterized Constructor: A constructor having at least one parameter is called as parameterized constructor. It can initialize each instance of the class to different values. 3.Copy Constructor: This constructor creates an object by copying variables from another object. Its main use is to initialize a new instance to the values of an existing instance.  4.Private Constructor: If a constructor is created with private specifier is known as Private Constructor. It is not possible for other classes to derive from this class and ...

Methods

 Static : No Need to Create object,Directly we call from class Void: Nothing will return only Executs and display Method: A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Why use methods?  1.To reuse code: define the code once, and use it many times. 2.Readability 3.Saves Memory syntax: <Access_Modifier> <Return Type> <Method Name>(Parameter List) {    Method Body } EX: public int FindMax(int num1, int num2) {       /* local variable declaration */       int result;       if (num1 > num2)          result = num1;       else          result = num2;       return result;    } Types: 1.Example Program Without Parameters & Without Return Type  2.Example Program Without Parameters & With Return Value Type 3.Example Program With Parameters ...

Loops

 Loops: In C#, loops are control flow structures that allow you to repeatedly execute a block of code based on a specified condition. Types 1.for 2.foreach 3.while 4.do-while 1.for: The for loop is used when you know the number of iterations in advance. Syntax: for (initialization; condition; iteration) {     // code to be repeated } Example: for (int i = 0; i < 5; i++) {     Console.WriteLine(i); } 2.foreach: The foreach loop is used to iterate over elements in an array or other enumerable collections. Syntax: foreach (var item in collection) {     // code to be repeated } Example: int[] numbers = { 1, 2, 3, 4, 5 }; foreach (var number in numbers) {     Console.WriteLine(number); } 3.while: The while loop is used when the number of iterations is not known in advance, and the loop continues as long as a specified condition is true. Syntax: while (condition) {     // code to be repeated } Example: int i = 0; while (i < 5) { ...

FileMethods

 var currentDirectory = System.IO.Directory.GetCurrentDirectory(); var filePath = @"\Resources\Templates\SOP.docx";  string filepath = currentDirectory + "" + filePath; var fileName = "BIMS_" + "" + mprnumber + ".docx"; currentDirectory = currentDirectory + "\\Resources\\Documents"; var filepath = Path.Combine(Path.Combine(currentDirectory), fileName); 1.To Read the Data From File byte[] bytes = System.IO.File.ReadAllBytes(filepath); // Read the contents of the file         string[] lines = File.ReadAllLines(filePath); 2.return File(bytes, contentType, fileName); 3.To Write Data into File System.IO.File.WriteAllBytes(printfilepath, bytes); // Write the updated contents back to the file         File.WriteAllLines(filePath, lines); 4.To Check Exist a File With Same Name System.IO.File.Exists(filepath) 5.To Delete a File System.IO.File.Delete(filepath); 6.To Copy a file  File.Copy(filepath, Outputpath);

Types of Classes

In C#, classes can be categorized into various types based on their functionality and usage. Here are some common types of classes in C#:   Standard or Concrete Classes Abstract Classes Static Classes Sealed Classes Partial Classes Nested Classes Generic Classes Anonymous Classes Partial Methods Extension Methods Standard or Concrete Classes : These are the most common types of classes used in C#. They define objects with specific attributes and behaviors. Examples include classes for modeling entities in a system, such as Person , Car , or Employee . Abstract Classes : Abstract classes cannot be instantiated directly; they serve as base classes for other classes. They may contain abstract methods that must be implemented by derived classes. Abstract classes are declared using the abstract keyword. Static Classes : Static classes cannot be instantiated, and they can only contain static members (fields, methods, properties). They are commonly used to define utility classes that pro...