C# CheatSheet

Questions regarding C# & ASP.NET

Contents

C# is a programming language

.Net is a framework that can run C# application

  1. It is a collections of library
  2. It has a CLR
1
2
3
4
5
6
7
            .Net framework      .Net Core           
Platform    Windows             Cross-platform      
Performance Slow                Better
CLI         IDE based           Full CLI command supported (Run CMD)
Packaging   One big framework   Delivered via modularly using Nuget

.Net5.0 is a unified platform

IL: Intermediate language code (partial compiled code) JIT: Just in time compiler

Source Code --> IL --(JIT)--> Machine code

Benefit: For different runtime and dev environment, JIT compiles the best optimized code for that environment

CLR: common language runtime

  1. CLR invokes JIT to convert IL code to machine language
  2. Cleans unused objects by using garbage collector

Background process which cleans Unused Managed resources Cannot clean unmanaged recources

Common types system ensures that data types defined in two different languages gets compiled to a common data type

Common language specifications A set of guidelines that make any language following .NET specifications (Case sensitive)

Stack stores primitive data types (int ,boolean, double) Heap stores objects

Stack handles function calls

Value types (contains actual data like int a = 0) are stores on stack Reference types (contains pointers and the pointers point to the actual data)are stored on heap

Explicit casting:(lower to higher type) int y = (int)d; Implicit casting:(lower to higher type) int i = 1; double d = i;

Strongly typed and flexible List geneint = new List(); geneit.Add(1);


  1. Simple & fast
  2. High scalability
  3. No buffering
  4. Cross plaforms
  1. Compliation of Source code
  2. Clubbing newly created code (Test code and funtion code)
  3. Executing assembly
  4. CLR (Common language runtime)
  1. Public: it can be accessed by every part of the code
  2. Private: it only can be accessed within the class
  3. Protected: it only can be accessed within the class and the inheriting class (parent and child class)
  4. Internal: it can be accessed in the class at the current assembly position
  1. Value parameters (not affect original value)
  2. Reference parameters (affect original value)
  3. Output parameters (returns more than one value)
  1. Final block is called after try catch blocks (not dependent on the exception handling)
  2. Finalize method is called just before garbage collection (cleanup operation for unmanaged code)

Managed code:

  1. Executed by CLR
  2. ALl the application code is dependent on .Net platform

Unmanaged code:

  1. Executed by runtime application of some other structure
  2. The runtime application deals with memory, security and other execution activities

Created when you want to restrict the class being inherited

  1. Make use of suplit function
  2. Splits the definition of the class into multiple classes in same file or multiple files
  3. One can create a class definition in multiple files but it is compiled as one class at run-time
  4. User can create object of partial class and access all the methods from every source file
StreamReader sr = new StreamReader("C: Readme.txt")
StreamWriter sr = new StreamWriter("C: Readme.txt")

/images/class-vs-struct.png

/images/virtual-vs-abstract.png

Used for organizing large code projects

Converting a value type to reference type is called boxing int value = 10 // Boxing: Object boxedvalue = value1;

// Unboxing:
int Unboxing = int(boxedvalue)

Array of arrays

Int[][] Jagarr = new[5][5];

int [] arr = {1,2}; ArrayList list = new ArrayList();

/images/array-vs-arraylist.png

A collection works like a container for instances of other classes, every class implements collection interface

Process that involves converting some code into its binary format

  1. Binay Serialization
  2. XML Serialization
  3. SOAP
String text = "200"
int.num = int.Parse(text)

A delegate is a variable that holds the reference to a method Useful to communicate between threads It’s a function pointer of the reference type

public Delegate int myDel(int number)
public int SubstractNum(int a) // Substract a by 10, return an int
// In public void start()
myDel DelegateExample = SubstractNum; // Holds the reference to a method

/images/string-vs-builder.png

Clone(): A new array obj is created with all elements

CopyTo(): All the elements get copied into another existing array

try
{
    int y = 0;
    int z = 1 / y;
}
catch(Exception ex) // parameter type is system.Exception
{
    // do something
}
  1. Used to make reusable code classes that decrease code redundancy
  2. Increase type safety, performance and optimization
  3. Generics create collection

Dispose releases unmanaged resources

Finalize doesn’t assure garbage collection (it releases unmanaged resources too)

Thread: is a set of instructions that when executed enables the program to perform concurrent processing. Thread terminates immediately after execution

Multithreading: Execute more than one process/task at a time

Parallel execution:

1
2
3
4
Thread t = new Thread(Method1);
t.Start();
Thread t1 = new Thread(Method2);
t1.Start();

TPL: Task parallel Library

  1. Parallel processing
  2. Pooling threads
  3. Perform actions with a task

Encapsulated delegate public Delegate void TestEvent(); public TestEvent TestEvent1

S: only a single thread will access in a given time S call waits for completion of method and then continuous the program flow

A: the method call immediately returns allowing the program to perform other operations

///

///


No comment yet.
Powered By Valine
v1.4.14