21 December 2013

Get factors of a number

int n = 10;
List<int> factors = new List<int>();
int max = Convert.ToInt32(Math.Sqrt(n));
for (int i = 1; i <= max; i++)
{
if (n % i == 0)
{
factors.Add(i);
if (i != max) // add square-root factors only once.
factors.Add(n / i);
}
}

Difference between String and string

1. String stands for System.String and it is a .NET Framework type.
2. string is a type in C#. System.String is a type in the CLR.
3. string is an alias for System.String in the C# language. Both of them are compiled to System.String in IL (Intermediate Language),
4. So there is no difference. Choose what you like and use that.
5. For code in C#, its prefer to use string as it's a C# type alias and well-known by C# programmers.


6. string is a reserved word, but String is just a class name.This means that 'string' cannot be used as a variable name by itself.


StringBuilder String = new StringBuilder(); // compiles
StringBuilder string = new StringBuilder(); // doesn't compile


7. If you really want a variable name called 'string' you can use @ as a prefix :
StringBuilder @string = new StringBuilder(); this applies to string also.


8. So String is not a keyword and it can be used as Identifier whereas string is a keyword and cannot be used as Identifier. And in function point of view both are same.
9. You can't use String without using System; but string can use without that namespace.
10. Static functions such as String.Format, String.Join, String.Concat, String.isNullOrWhitespace etc... are from String.



I can say the same about (int, System.Int32) etc..

20 December 2013

default Timeouts in c#

SqlConnection.ConnectionTimeout - 15 sec

- Gets the time to wait while trying to establish a connection before terminating the attempt.

SqlCommand.CommandTimeout - 30 seconds

- Gets or sets the wait time before terminating the attempt to execute a command

Session Timeout - 20 mins

<system.web>
<SessionState timeout=”10”></SessionState>

Cookies per Domain - 20 - 1KB

 

PrimaryKey vs UniqueKey

Primary Key:
Primary Key enforces uniqueness of the column on which they are defined.
Primary Key creates a clustered index on the column.
Primary Key does not allow Nulls.

CREATE TABLE EMP (eID INT NOT NULL PRIMARY KEY,
eName VARCHAR(100) NOT NULL)

Alter table with Primary Key:
ALTER TABLE EMP
ADD CONSTRAINT pk_eID PRIMARY KEY (eID)

Unique Key:
Unique Key enforces uniqueness of the column on which they are defined.
Unique Key creates a non-clustered index on the column.
Unique Key allows only one NULL Value.

Alter table to add unique constraint to column:
ALTER TABLE EMP ADD CONSTRAINT UK_empName UNIQUE(empName)

Refer: https://www.simple-talk.com/sql/learn-sql-server/primary-key-primer-for-sql-server/

String Vs StringBuilder

System.String
-------------
1. String is immutable. It means that you can't modify string at all, the result of modification is new string. This is not effective if you plan to append to string
2. many actions that you do with string
3. Here concatenation is used to combine two strings by using String object
4. The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old string is deleted
5. string is non updatable
6. everytime a object has to be created for Operations like append,Insert etc. at runtime

System.StringBuilder
--------------------
1. StringBuilder is mutable. It can be modified in any way and it doesn't require creation of new instance.
2. if you try to do some other manipulation (like removing a part from the string, replacing a part in the string, etc.), then it's better not to use StringBuilder at those places. This is because we are anyway creating newstrings.
3. system.stringbuilder is updateble
4. string builder is faster than the string object
5.String builder is more efficient in case large amount of string operations have to be perform.
6.Insertion is done on the existing string.
7. At the end, we can get the string by StringBuilder.ToString()
dis-adv:
1. We must be careful to guess the size of StringBuilder. If the size which we are going to get is more than what is assigned, it must increase the size. This will reduce its performance.
2. many actions can't be done with StringBinder.
3. When initializing a StringBuilder, you are going down in performance.
4. StringBuilder can be used where more than four or more string concatenations take place.