DOT NET – Difference between string and string builder

In Dot Net there will be many cases where in we need to operate on strings and first thing we remember is using system.String but there are certainly many points we need to remember and consider before we operate on strings.

1) Performance.

2)how many times we need to concatenate.

Lets take an example for concatenate five strings.

EX 1. Using System.String

System.String str =“My Name is dilip”;

str += “and i am”; str += “working on “;

str += “Post of “;

str += “difference between string and string builder”;

Response.Write(str);

and the expected output well you probably guessed it right

“My Name is dilipand i amworking on Post of difference between string and string builder”

Now what has happed? yes the important question now how many times we have appended the str variable those number of times string was created in memory location and abandoned when a new string is created and later waiting for garbage collection.

This leads to memory wastage and degradation of performance because string are immutable(that means any change to string causes a runtime to create a new string and abandon old one).

Think about the situation where in u need to work on 100 or more strings????

Dot Net has answer for it in the form of System.Text.StringBuilder class

EX2. Same Example using StringBuilder

StringBuilder sb = new StringBuilder();

sb.Append( “My Name is dilip”);

sb.Append( “and i am”);

sb.Append( “working on “);

sb.Append( “Post of “);

sb.Append( “difference between string and string builder”);

and the output is same as previous

“My Name is dilipand i amworking on Post of difference between string and string builder”

But this time there was only one string created in memory dynamically and modified as we append the new string, by this there is not much garbage collection and also helps improve performance. Append is taken only for example there are a lot of other functions which are just waiting for you to invoke.Happy coding.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Advertisements

DOT NET 4.0 – Design goals for C#.NET

The ECMA standard lists these design goals for C#:

  • C# is intended to be a simple, modern, general-purpose, object-oriented programming language.
  • Because software robustness, durability and programmer productivity are important, the language should include strong type checking, array bounds checking, detection of attempts to use uninitialized variables, source code portability, and automatic garbage collection.
  • The language is intended for use in developing software components that can take advantage of distributed environments.
  • Programmer portability is very important, especially for those programmers already familiar with C and C++.
  • Support for internationalization is very important.
  • C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DotNet FrameWork – What actually happens when you add something to an arraylistcollection – How Boxing and unboxing occures in memory – Why only boxed types can be unboxed – Day 30 of 30

What actually happens when you add  something to an arraylistcollection ?

Following things will happen :

Arraylist is a dynamic array class in c# in System.Collections namespace derived from interfaces – ICollection , IList , ICloneable , IConvertible  . It terms of in memory structure following is the implementation .

a. Check up the total space if there’s any free space on the declared list .

b. If yes add the new item and increase count by 1 .

c. If No Copy the whole thing to a temporary Array of Last Max. Size .

d. Create new Array with size ( Last Array Size + Increase Value )

e. Copy back values from temp and reference this new array as original array .

f. Must doing Method updates too , need to check it up .

What is Boxing and unboxing? Does it occure automaatically or u need to write code to box and unbox?

Boxing – Process of converting a System.ValueType to Reference Type , Mostly base class System.Object type and allocating it memory on Heap .Reverse is unboxing , but can only be done with prior boxed variables.

Boxing is always implicit but Unboxing needs to be explicitly done via casting , thus ensuring the value type contained inside .

How Boxing and unboxing occures in memory?

Boxing converts value type to reference type , thus allocating memory on Heap . Unboxing converts already boxed reference types to value types through explicit casting , thus  allocating memory on stack .

Why only boxed types can be unboxed?

Unboxing is the process of converting a Reference type variable to Value type and thus allocating memory on the stack . It happens only to those Reference type variables that have been earlier created by Boxing of a Value Type , therefore internally they contain a value type , which can be obtained through explicit casting . For any other Reference type , they don’t internally contain a Value type to Unboxed via explicit casting . This is why only boxed types can be unboxed .

Please read all the post in the Dotnet Framework series.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DotNet FrameWork– Dot Net Basics – Day 6 of 30

How big is the datatype int in .NET?
32 bits.
How big is the char?
16 bits (Unicode).
How do you initiate a string without escaping each backslash?
Put an @ sign in front of the double-quoted string.
What’s the access level of the visibility type internal?
Current application.
Explain encapsulation ?
The implementation is hidden, the interface is exposed.
What data type should you use if you want an 8-bit value that’s signed?
sbyte.
Speaking of Boolean data types, what’s different between C# and C/C++?
There’s no conversion between 0 and false, as well as any other number and true, like in C/C++.
Where are the value-type variables allocated in the computer RAM?
Stack.
Where do the reference-type variables go in the RAM?
The references go on the stack, while the objects themselves go on the heap.

What is the difference between the value-type variables and reference-type variables in terms of garbage collection?

The value-type variables are not garbage-collected, they just fall off the stack when they fall out of scope, the reference-type objects

are picked up by GC when their references go null.
How do you convert a string into an integer in .NET?
Int32.Parse(string)
How do you box a primitive data type variable?
Assign it to the object, pass an object.
Why do you need to box a primitive variable?
To pass it by reference.
What’s the difference between Java and .NET garbage collectors?
Sun left the implementation of a specific garbage collector up to the JRE developer, so their performance varies widely, depending on whose JRE you’re using. Microsoft standardized on their garbage collection.
How do you enforce garbage collection in .NET?
System.GC.Collect();
What’s different about namespace declaration when comparing that to package declaration in Java?
No semicolon.
What’s the difference between const and readonly?
You can initialize readonly variables to some runtime values. Let’s say your program uses current date and time as one of the values that won’t change. This way you declare public readonly string DateT = new DateTime().ToString().
What happens when you encounter a continue statement inside the for loop?
The code for the rest of the loop is ignored, the control is transferred back to the beginning of the loop.
What’s the advantage of using System.Text.StringBuilder over System.String?
StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it’s being operated on, a new instance is created.
Can you store multiple data types in System.Array?
No.
What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
The first one performs a deep copy of the array, the second one is shallow.
How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
What’s the .NET datatype that allows the retrieval of data by a unique key?
HashTable.
What’s class SortedList underneath?
A sorted HashTable.
Will finally block get executed if the exception had not occurred?
Yes.
Can multiple catch blocks be executed?
No, once the proper catch code fires off, the control is transferred to the finally block (if there are any), and then whatever follows the finally block.
Why is it a bad idea to throw your own exceptions?
Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.
What’s a delegate?
A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.
What’s a multicast delegate?
It’s a delegate that points to and eventually fires off several methods.

Please read all the post in the Dotnet Framework series.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DotNet FrameWork– difference between Finalize and Dispose (Garbage collection) – Partial Assembly References– Reflection – Basics Day 4 of 30

What is the difference between Finalize and Dispose (Garbage collection) ?

Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize Method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object. In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object.

Dispose can be called even if other references to the object are alive. Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from permanently leaking if the programmer fails to call Dispose.

What is Partial Assembly References?

Full Assembly reference: A full assembly reference includes the assembly’s text name, version, culture, and public key token (if the assembly has a strong name). A full assembly reference is required if you reference any assembly that is part of the common language runtime or any assembly located in the global assembly cache.

Partial Assembly reference: We can dynamically reference an assembly by providing only partial information, such as specifying only the assembly name. When you specify a partial assembly reference, the runtime looks for the assembly only in the application directory.We can make partial references to an assembly in your code one of the following ways:

-> Use a method such as System.Reflection.Assembly.Load and specify only a partial reference. The runtime checks for the assembly in the application directory.

-> Use the System.Reflection.Assembly.LoadWithPartialName method and specify only a partial reference. The runtime checks for the assembly in the application directory and in the global assembly cache

Changes to which portion of version number indicates an incompatible change?

Major or minor. Changes to the major or minor portion of the version number indicate an incompatible change. Under this convention then, version 2.0.0.0 would be considered incompatible with version 1.0.0.0. Examples of an incompatible change would be a change to the types of some method parameters or the removal of a type or method altogether. Build. The Build number is typically used to distinguish between daily builds or smaller compatible releases. Revision. Changes to the revision number are typically reserved for an incremental build needed to fix a particular bug. You’ll sometimes hear this referred to as the “emergency bug fix” number in that the revision is what is often changed when a fix to a specific bug is shipped to a customer.

Please read all the post in the Dotnet Framework series.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )