Skip to main content

Posts

#C# String is Immutable in C#

There is a term called immutable, which means the state of an object can't be changed after is has been created.  A string is an immutable type. The statement that a string is immutable means that, once created, it is not altered by changing the value assigned to it. If we try to change the value of a string by concatenation (using + operator) or assign a new value to it, it actually results in creation of a new string object to hold a reference to the newly generated string. It might seem that we have successfully altered the existing string. But behind the scenes, a new string reference is created, which points to the newly created string. string s1="Hi"; string s2=s1; s2=s2+"Raj"; s1  -> pointer to data "Hi" s2  -> pointer to date "Hi" -> pointer to data "Hi Raj" here s2 is pointing to new data. that is s2 pointer is changed.(Immutable)

#c# String is a reference type. Why?

The data type Integer is a  value type , but a String is a  reference type .  Why ? A String is a reference type even though it has most of the characteristics of a value type such as being immutable and having == overloaded to compare the text rather than making sure they reference the same object. Why a string is just a value type Why isn't a string just a value type, then?   First of all let's understand what value types and reference types are and the difference between them in a glance. The types can be classified depending on whether a variable of a specific type stores its own data or a pointer to the data. That means, instead of storing data, it stores its address. I f it stores its own data it is a value type and if it holds a  pointer  to data  elsewhere  in memory it is a reference type. Value types are stored in the Stack whereas reference types are stored in the Heap.

#MVC : Why does Html.Label() not work with periods? or Why is @Html.Label() removing some characters

You are misusing the  Html.Label  method. It is for: Returns an HTML label element and the  property name of the property  that is represented by the specified expression. That's why it gets confused if you have a point  .  in the first parameter because it expects a property expression there. However, you can use the second overload: @Html . Label ( "" , String . Format ( "{0}. someText" , 1 )) Or just write out the HTML: <label> @String . Format ( "{0}. someText" , 1 )</ label > or <label class="WelcomeText" style="float: left">Welcome @Html.Label("", Model.USERID + " ( " + Model.ROLE + " )", new { @class = "WelcomeText" })</label> 

#SQL: How to Drop Database in SQL Server by Closing Existing Connections

How to Drop Database in SQL Server when the users are connected to a SQL Server Database. You may need to closing existing connections in a database before restoring the database, before detaching database, get database in SINGLE_USER mode etc. If you try dropping a database when users are connected to the SQL Server Database you will receive the below mentioned error message. Error Message Drop failed for Database 'RajDB'. (Microsoft.SqlServer.Smo) Cannot drop database "" because it is currently in use. (Microsoft SQL Server, Error: 3702) USE [master] GO ALTER DATABASE RajDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO /* Query to Drop Database in SQL Server  */ DROP DATABASE RajDB GO

#EF : DbEntityValidationException - How can I easily tell what caused the error?

While calling  SaveChanges  on my  DbContext , I get the following exception: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. This is all fine and dandy, but I don't want to attach a debugger every time this exception occurs. More over, in production environments I cannot easily attach a debugger so I have to go to great lengths to reproduce these errors. How can I see the details hidden within the  DbEntityValidationException ? Answer :  The easiest solution is to override SaveChanges on your entities class. You can catch the DbEntityValidationException, unwrap the actual errors and create a new DbEntityValidationException with the improved message. Create a partial class next to your SomethingSomething.Context.cs file. Use the code at the bottom of this post. That's it. Your implementation will automatically use the overriden Save...

#AT Repository and Unit of Work Pattern - 7

Presentation project create a WCF service application by right clicking on solution. we will get Service1.svc and IService1.svc. rename the service if required. change the web config. <endpointBehaviors>         <behavior>           <webHttp />         </behavior>       </endpointBehaviors> <services>       <service name="ContactMangWCF.ContactManagerService" behaviorConfiguration=" myBehaviour ">         <endpoint address="" binding="webHttpBinding" contract="ContactMangWCF.IContactManagerService"></endpoint>       </service>     </services> <serviceBehaviors>         <behavior name=" myBehaviour ">           <!-- To avoid disclosing metadata information, set the values below to false before deployment...