About

Sunday, September 29, 2019

Basic garbage collection

In the common language runtime (CLR), the garbage collector serves as an automatic memory manager. 
It provides the following benefits:
  • Enables you to develop your application without having to manually free memory for objects you create.
  • Allocates objects on the managed heap efficiently.
  • Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field.

Garbage Collection in C# Interview Question 


What is Garbage Collection

Garbage collection is one of most important future in the .Net Framework environment

Futures : 

1. When we have a class that represents an object in the runtime that allocates a memory space in the heap memory.

2. When there isn't enough memory to allocate an object, the GC must collect and dispose of garbage memory to make memory available for ne allocations


3. You can force garbage collection either to all the three generations or to a specific generation using the GC.Collect() method the GC.Collect() Method is overloaded you can call it without any parameters or event by passing the generation number you would like to the garbage collector to collect.

4 it determines when an instanciated class is no longer in using and reclaims memory.

5. The .Net  language does not do so the CLR does so. that's why its called "automatic garbage collection"

There are three type in generation :



Wednesday, September 25, 2019

.Net Core C# Revers String functionality without using .string.revers function 
: Interview questions 

Without using any .net revers string functionality. we can develop our own revers string function with using below code example


Example Code

static StringBuilder stringBuilder = new StringBuilder();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Prasad KM");
           
            ReverseString("PrasadKM", 8);
            Console.WriteLine("First time string(PrasadKM) revers string :  " + stringBuilder.ToString());
         

string tempdata = stringBuilder.ToString();
            stringBuilder = new StringBuilder();

            ReverseString(tempdata, 8);
            Console.WriteLine("Second time string(MKdasarP) revers string :   " + stringBuilder.ToString());
            Console.Read();
        }

//Reflection Method
        static void ReverseString(string str, int len)
        {
            if (len <= 0)
                return;
            stringBuilder.Append(str[len - 1]);
            ReverseString(str, --len);
        }

//For each functionality
 static void ReverseString1(string str)
        {
           
            for (int i = str.Length-1; i <= str.Length; i--)
            {
                if (i < 0) break;
                stringBuilder.Append(str[i]);
            }
        }

OutPut


Conclusion : Without using .net string revers functionality, we can develep the our own revers string functionality with above code.

Please add your comments:

Tuesday, September 24, 2019

Execute one async and normal method  C#


We have two methods in below example 
1. Without await functionality with async key word
2. Without await and async method/functionality(normal method )

The below example is  methods will be executing in  asynchronous action.

Example : 
 static void Main(string[] args)
        {
            GetTaskAsync();
            NormalMethod();
            Console.Read();
        }
        static async Task  GetTaskAsync()
        {
            await Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(" GetTaskAsync");
                }
            });
        }

        static void NormalMethod()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(" NormalMethod");
            }
        }
    }

OutPut : 

Conclusion : As per above points with async and with out await key words the functionality will work like asynchronous

Please add the comments then I can improve the blog thank you


Async and await  C# .Net


What is Async C#?

async functionality/methods runs/executes until it reaches it's first await functionality and until the awaited task is complete.

Async key will support only when it modifies a method and lambda expression or anonymous functionality.

Example 1 :

static void Main(string[] args)
        {
            Int32 VariableName = 0;
            Console.WriteLine("GetTaskAsync1 - Started  Thread ID " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            GetTaskAsync1();
            Console.WriteLine("GetTaskAsync1 - End  Thread ID " + System.Threading.Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

        static async Task GetTaskAsync1()
        {
            Console.WriteLine("GetTaskAsync2 - Started  Thread ID " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            int result = await GetTaskAsync2();
            Console.WriteLine("result : "+ result);
            Console.WriteLine("GetTaskAsync2 - End  Thread ID " + System.Threading.Thread.CurrentThread.ManagedThreadId);
        }
        static async Task<int> GetTaskAsync2()
        {
            int count = 0;
            for (int i = 0; i < 1000; i++)
            {
                count++;
            }
            return count;
        }
    }

Conclusion :  if we use async and await key then the functionality will run in synchronous action and the functionality will run in background thread.


Please add the comments then I can improve the blog thank you