Advantages and Disadvantages of using view state for state management

Home ASP.Net & C#
MVC
Others




What are the Advantages and Disadvantages of using view state for state         management 
        
     Advantages
  • View state is easy to implement. We can easily enable or disable view state at the page level or at the controller level
  • They are not using any server side resources. It is stored in the client side browser in a hidden field.
  • View state is secure because it stores the data in encrypted format.
  • It is good with HTTP data transfer      
     Disadvantages
  • View state information cannot transfer across pages automatically.
  • If the view state information is very large, it may cause the page load/ post back to be slow.
  • Mobile devices might not have the enough memory capacity to store a large view state data.

What is the difference between String and StringBuilder in C# with Example

Home ASP.Net & C#
MVC
Others

What is the difference between String and StringBuilder in C# with Example

Introduction
         As we all know, in C# we have string and StringBuilder for string manipulation. Have you ever think of why we need these 2. Everything that we can do with StringBuilder can be done with string also. Yes. But there is one important scenario where we need to use StringBuilder instead of string.

         Most of us are usually using string in our code when we need to do any manipulation on string data. Usage of string and StringBuilder plays an important role when it comes to performance. There is a serious performance issue when we are using string for concatenation which can be overcome by using StringBuilder. Lets check both of them and see how it is.

String
         We can simply say string is immutable. So what is immutable means. Immutable means, once we created a string object, we cannot modify it. All the operations on that string ( Replace, append, modification etc) will discard the old object and create a new instance in the memory. 













This is actually equal to creating 100 strings.

         Just keep in mind this about string for now. And we can now see what StringBuilder is and after that we can see how StringBuilder is important for performance. 

StringBuilder
         StringBuilder is mutable. This means, once we create a StringBuilder object and any operations performed on that object will not create new object in memory. So when it comes to performance, if we have many append operation on a string, we need to go for StringBuilder instead of string. 







         


         Another important thing about StringBuilder is , when we are creating a StringBuilder object without specifying the capacity, it will have a capacity of 16 characters by default. And if we are trying to append more than one character after 16, it will discard the old StringBuilder object and create a new one. The newly created object will have double capacity than the old one. That is 32 characters. And appending string more than 32 characters will create an object with 64 characters. See the code below.





         Here I have declared a StringBuilder with specifying its capacity as 10000 characters. So it will create an object in memory with capacity of 10000 characters. And if we append a character more than 10000, it will create a new object with 20000 characters. So it is always a performance booster if we can able to specify approximate size of the characters that we are going to save in StringBuilder object. 

         I can personally say, it is simply a non sense to create a StringBuilder object without specifying the number of characters. Also if we are using StringBuilder for concatenating 2 or 3 strings, it is also useless. Using string is better here. Because, considering the performance overhead of creating StringBuilder over string, it is advisable to create StringBuilder if you have more than 5 string concatenation.( Personal opinion )

Performance comparison using string and StringBuilder

         Now we can check whether the above said regarding performance is true if we are using StringBuilder. :) . 

I am using a string object to concatenate 25000 strings. I am displaying the time before concatenation starts and after concatenation done. 













    And the output of above code is..









It took almost 4 seconds to complete the concatenation. 

Now we can use StringBuilder for the same. See the code below













The output of the above code is 











See this.. the StringBuilder object did not take even 1 second for concatenation. Here we can see the performance impact when we are using string for large concatenations.

I hope you all understood the differences and the places where we need to use StringBuilder.

We can say the differences between string and StringBuilder as 
  • String is immutable, StringBuilder is mutable. 
  • StringBuilder shows more performance than string
  • string belongs to System namespace. But StringBuilder belongs to System.Text namespace. 



.Net Interview Questions And Answers


Home ASP.Net & C#
MVC
Others

In this blog, I will share some ideas, tutorials, Interview questions and Answers for .net Programmers. There are a lots of articles and Interview questions which I am preparing and is not completed yet. So please view the blog continuously to get all the updates :) 

Search : 

11 . What is the difference between view state and hidden field?

  • View state stores data in encrypted format, but hiddent field does not encrypt data. Because of this view state is secure but hidden field is insecure.
  • View state can store large amount of data. But hidden field store small amount of data.
12. What is an IL?
      Intermediate Language is also known as Microsoft Intermediate Language (MSIL). All the .Net source code is compiled to IL. IL is machine independent. At the time of installation only it knows in which environment it is going to install. At the point when the s/w is installed or at run time, this IL is converted to machine code by Just in Time (JIT) compiler

13. What is  JIT
      JIT stands for Just In Time compiler which is an important feature of .net. JIT converts IL to machine code at the time of installation of the software or at runtime.

14. What is Assembly Manifest ?
       Assembly metadata is stored in Manifest. Manifest contains the flollowing things.
  • Version of assembly.
  • Scope of the assembly
  • Security information.
  • References to other assemblies and classes
15. What is the difference between Primary key and unique key?
  • Primary key does not allow nulls. But unique key allow one null value in the column.
  • By Default primary key creates clustered index on the column but unique key creates a non clustered index by default.
16.  What is the difference between .toString() and Convert.toString()?
       
       .toString() does not handle nulls. But Convert.toString() handle null and return empty but .toString() will throw null reference exception if null value comes. So as a good practice it is better to use Convert.toString()

17. What is meant by page rendering ?
      Page rendering is nothing but just converting the .aspx (server) controls to HTML controls for sending it to client browser.

18. What is boxing and unboxing in .net?
      Boxing is the process of converting value type to reference type. Unboxing is the process of converting reference type to value type

19. Can we force garbage collector to run ?

      Yes. We can do this by System.GC.Collect();

20. How Can I block finally block to be executed after the try block?
      
      Use System.exit (0); at the end of try block. It will make the control to go outside the executing program. And the finally block will not get executed.



ASP.Net Concepts And Tutorials

Home ASP.Net & C#
MVC
Interview Questions & Answers
Others



In this blog, I will share some ideas, tutorials, Interview questions and Answers for .net Programmers. There are a lots of articles and Interview questions which I am preparing and is not completed yet. So please view the blog continuously to get all the updates :) 

Search : 

List of Articles in ASP.Net


What is value type and reference type in C# .net with Example

What is the difference between Classic ASP and ASP.Net

How To Make the header of a grid fixed ( Freeze Header )

How to make the grid header fixed ( Freeze Header ) and fill data from a datatable

What you mean by assembly in .Net

SQL Server

Home ASP.Net & C#
MVC
Others



In this blog, I will share some ideas, tutorials, Interview questions and Answers for .net Programmers. There are a lots of articles and Interview questions which I am preparing and is not completed yet. So please view the blog continuously to get all the updates :) 

Search : 

List of Articles in SQL Server

How To split a string in SQL Server

.Net Interview Questions And Answers


Home ASP.Net & C#
MVC
Others

In this blog, I will share some ideas, tutorials, Interview questions and Answers for .net Programmers. There are a lots of articles and Interview questions which I am preparing and is not completed yet. So please view the blog continuously to get all the updates :) 

Search : 

1. What is state management in ASP.Net
         State management means preserving the state of the page, controls, objects, user etc while we are moving from one page to another or a post back happens.

2. Why we need state  management in Asp.net
         Data is transferred from server to the browser over HTTP. HTTP is a stateless protocol. Stateless means, state of the web page, controls are lost once we post the page from client to the server.It is important to save the state of the controls in a page after the page is post back.
         For example I have one 1 text box for entering the "Employee Name" and one submit button for saving the "Employee Name". When click on the submit button, data is posted back to the server and data is processed at the server side. Since HTTP is stateless, it will not keep the value in the text box (Emplyee Name) after the postback. Here, for maintaining the value in the textbox after post back we need to use state management techniques.

3. What are the different types of state management in ASP.Net
         There are 2 types of state management techniques
  • Server Side
  • Client Side

4. What are the different types of Client side state management in ASP.Net
  • View State
  • Hidden Field
  • Cookies
  • Control State
  • Query Strings
5. What are the different types of Server side state management in ASP.Net
  • Session
  • Application
6. From the list below, which is the control for which by default post back is         enabled.
  • button
  • textbox
  • listbox
  • gridview
     Ans - Button ( By Default button control have post back is enabled )

7. What are the Advantages and Disadvantages of using view state for state         management 
        
     Advantages
  • View state is easy to implement. We can easily enable or disable view state at the page level or at the controller level
  • They are not using any server side resources. It is stored in the client side browser in a hidden field.
  • View state is secure because it stores the data in encrypted format.
  • It is good with HTTP data transfer      
     Disadvantages
  • View state information cannot transfer across pages automatically.
  • If the view state information is very large, it may cause the page load/ post back to be slow.
  • Mobile devices might not have the enough memory capacity to store a large view state data.
8. What are the Advantages and Disadvantages of using Hiddenfield for state management ?

     Advantages

  • Hidden fields are standard HTML controls and it does not require any complex programming logic.
  • Almost all the browsers supports hidden field.
  • The hidden field can be stored and read from the page.
     Disadvantages
  • Hidden field value is stored in the page itself and can directly view the content if we view the page source. This is potential security issue.
  • Hidden field value is stored in the page. Storing big values make the page load/ post back to be slow.
  • Hidden field stores value in the string format and it does not support rich data types. So we need to use custom logic ( like implement delimited strings) for storing multiple values
9. What are the Advantages and Disadvantages of using Session for state management ?

     Advantages
  • Easy to implement
  • Accessing the data is fast since it is stored in memory
  • Complex data types can be stored in session without serialization
  • When we are using out proc mode, session data can be shared across servers and will not lose the data even the IIS or worker process restarts.
     Disadvantages
  • Since it is stored in the memory, storing large data will impact the performance.
  • When we are using in proc mode, IIS restart or worker process restart will cause the data to be lost.
  • Web garden or Web farm cannot use the session state when using In proc mode
10. What are the Advantages and Disadvantages of using Query String for state management ?

     Advantages
  • Easy to use
  • Supported by almost all the browsers
  • No extra effort is needed to code
     Disadvantages
  • Since the query string values are appended at the end of url, it is easy to view by the users - Which is a security breach
  • Most of the browsers specify a maximum limit on the characters in the query string ( In IE, it is 2083 characters)



Dot Net Concepts and Interview Questions

What is value type and reference type in C# .net with Example

Home ASP.Net & C#
MVC
Interview Questions & Answers
Others
What is value type and reference type in C# .net with Sample code

Introduction 

        As a dot net developer, it is very much important to understand what is happening when we are declaring a variable. For creating an application which uses memory effectively and with better performance, we need to understand how memory is allocated while we are declaring a variable.

        When we are declaring a variable in C#, it allocates some memory in RAM for that variable. There are 3 parts for this memory

1. Name of the variable
2. Value of the variable and
3. Data type of the variable.
That is it can be like below

int x = 1;







A little deeper


        This is very basic understanding. If we go a little deeper, we can see there are 2 types of memory is available for storing the variable
1. Stack     and 2. Heap
        So for now just understand - the variable you are declaring will be stored in any one of this memory, depending on the data type that you are declaring. We can skip stack and heap for now.

Now lets come to our point


What is Value type

        Variables that are stored in the stack memory are called value types. Value types contain their actual data. With value types, each variable have its own copy of data. So operations on one variable does not affect the other variable.( you can understand this point after reading reference type also ).
Consider the example below ( I suggest you to try this code yourself and get better understanding)





















The out put of the above code is









         This is one important point in value types. In line 2, I have assigned x to y and in line 3, I have updated the value of x to 5. But that is not affecting the value of y. This is because value type variables have their own copy of data. You can get better understanding while you complete reference type also.


        There is one more important thing for value types. After executing the method, the memory allocated for the value types are automatically get unallocated. That is immediately after the scope of the value type variables, memory will be cleared. They will not wait for the Garbage collector to run.

        All the built in data types of .net  excluding string and object are value types. Also, among the user defined data types, struct and enum are value types.
Example : int, long, float, double, decimal, byte, short, char, enum, struct etc.


What is reference type

       Now lets come to reference types. Unlike value type, reference type does not store the data directly.Instead they contain pointers and pointers will point to the actual data. Reference types are stored in heap memory. This can be like below














        So variable x contain the pointer to the memory location 100 and y points to 101. This is how reference types are storing the data. So, now it is possible that both x and y can point to same location say 100. Yes this is possible. See below. 














        Now what happens is, operations on one variable will affect the other variable, which is pointing to the same. That is, if x is going to change its value "Hello" to "Hello World", then what is going to happen here is it will change the value in the memory location 100 to "Hello World". After this operation if we check the value of y, we can see that value of y also got changed to "Hello World" because y is also pointing to the same location 100. 


        Therefore, 2 variables can reference a single( same ) object and operations on one variable can affect the other. We can see this change with example.























The output of the above will be 







We can see that both x and y have the same value even I have changed the value of x only. 


        One other thing we need to remember in case of reference types is it will not reclaim the memory immediately after the variable goes out of the scope. We need to wait for the garbage collector to run for unallocating the caimed memory.



Examples : string, object, class, array, delegate, interface etc. 

So we can conclude value types and reference types as



  • Value types contain the actual data while reference types will store reference to the data
  • Value types are stored in stack while reference types are stored in heap
  • Garbage collector is not required to unallocate the memory in the case of value type. But for reclaiming the reference types memory, we need wait for the Garbage collector to run.