-4

how to create Primary key with a specific name when creating the table itself.

I wanted to know if we can create Primary Key as part of the table name as well, and also give it a name at the same time. Though it would look very normal to all experienced developers, it can be still confusing to many.

What am I doing wrong?

CREATE TABLE [dbo].[TestTable]( [ID] [int] IDENTITY(1,1) NOT NULL, [FirstName] [varchar](100) NULL) GO ALTER TABLE [dbo].[TestTable] ADD CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED ([ID] ASC) GO

Thanks,Mahendra

Mahendra Sahu
  • 80
  • 1
  • 11
  • for: http://msdn.microsoft.com/en-us/library/ch45axte.aspx foreach: http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx – Spontifixus Dec 04 '12 at 18:26
  • lots of things are different; they can potentially be used to do similar things, but there are probably as many differences as there are similarities. Just look up the definition of what each one does. – Servy Dec 04 '12 at 18:26
  • 1
    What is "C# asp.net?" I'm not familiar with any such language. – cdhowie Dec 04 '12 at 18:27

3 Answers3

4

foreach enumerates any object that implements IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T> (or actually any object whose class implements a compatible Current property and MoveNext() method). The syntax for foreach is:

foreach (type variable in enumerable) { ... }

type is the type of the new variable variable. (If you already have an existing variable to use then omit the type.)

The body of the loop will be executed for each object in the enumerable, and the iteration variable (here variable) will contain the object on each iteration.

for is much more general-purpose, and follows this pattern:

for (initial-statements; loop-condition; post-iteration-actions) {
    body
}

Which is almost exactly equivalent to:

initial-statements
while (loop-condition) {
    body
    post-iteration-actions
}

You may see a for loop when dealing with arrays, for example:

for (int index = 0; index < array.Length; index++) {
    Console.WriteLine(array[index]);
}

This is the same thing as:

{
    int index = 0;
    while (index < array.Length) {
        Console.WriteLine(array[index]);
        index++;
    }
}

(Note that using foreach over arrays is optimized by the compiler to use a similar index-based iteration, since this is faster. So all three approaches should have equivalent performance and runtime semantics when enumerating an array.)


Further information, if you want it:

foreach is actually just syntactic sugar, and can be easily translated into a while loop.

foreach (object x in y) {
    Console.WriteLine(x);
}

This actually means:

var enumerator = y.GetEnumerator();
try {
    object x; // See footnote 1
    while (enumerator.MoveNext()) {
        x = enumerator.Current;
        Console.WriteLine(x);
    }
} finally {
    ((IDisposable)enumerator).Dispose();
}

Except that the enumerator variable has no name and is hidden from you.

It's pretty obvious why foreach exists. Everywhere you use it today you would otherwise have to write this horribly verbose boilerplate code. foreach takes care of everything for you -- grabbing an enumerator, testing MoveNext() on each iteration, extracting the enumerator's current value, and disposing of the enumerator after iteration completes.

1 This variable is actually semantically declared inside of the loop, starting with C# 5. This example is accurate for C# 4 and predecessors.

Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300
0

foreach is used on top of collections to traverse through while for can be used on anything for the same purpose.

Lior
  • 5,841
  • 9
  • 32
  • 46
0

Foreach uses an iterator, for uses an index

so

foreach(var item in myCollection)
{
   Console.WriteLine(item);
}

versus

for(int i = 0; i< myCollection.Count; i++)
{
    Console.WriteLine(myCollection[i]);
}

So if that these were the meat of a Function called PrintContent, one will take anything that implements IEnumerable the other the argument must have a count property and implement and indexer of type integer. When you throw sorts and filters into the mix it gets even less amusing, it's all about abstractions, PrintContent doesn't need to know how myCollection is implemented just that myCollection implements IEnumerable.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39