Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi
Sir, I can't understand this code. Please help me.
C#
using System;
using System.Collections;
using System.Collections.Generic;

class Test
{
    //Prints the contents of any generic Stack by using generic type inference
    public static void PrintStackContents<T>(Stack<T> s)
    {
        while(s.Count != 0)
        {
            Console.WriteLine(s.Pop());
        }
    }

    public static void Main(String[] args)
    {
        Stack<int> s2 = new Stack<int>();
        s2.Push(4);
        s2.Push(5);
        s2.Push(6);

        PrintStackContents(s2);

        Stack<string> s1 = new Stack<string>();
        s1.Push("One");
        s1.Push("Two");
        s1.Push("Three");

        PrintStackContents(s1);
    }
}

And also, what is the use of Anonymous Types in C#?
Posted
Updated 5-Mar-11 8:08am
v2

Just my addition to point out how important anonymous methods are: they are a big step into functioning programming.

First, please see my Tips/Trick article on hiding methods in other methods: Hide Ad-hoc Methods Inside the Calling Method’s Body[^].

Then, good style of programming event handler is using anonymous methods: how to call keydown event on particular button click[^] (also, pay attention to lambda notation as a convenient alternative).

The anonymous methods are important in using of invocation mechanism for UI threads, both WPF and System.Windows.Forms, Control.Invoke() vs. Control.BeginInvoke()[^], this mechanism can be used in more general case for a custom thread, see my Tips/Trips article on the topic: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Also, just to understand that not everything is easy, get an idea of closures (http://en.wikipedia.org/wiki/Closure_(computer_science)[^]), which only can be implemented using anonymous methods, see another Answer: Looping for creating new thread give some problem[^].

—SA
 
Share this answer
 
v3
Comments
sairam.bhat 10-Mar-11 0:29am    
Thank you
Sergey Alexandrovich Kryukov 10-Mar-11 0:33am    
You're welcome.
Thanks for accepting my Answer.
--SA
The PrintStackContents method:
C#
public static void PrintStackContents<T>(Stack<T> s)
    {
    while(s.Count != 0)
        {
        Console.WriteLine(s.Pop());
        }
    }
Is a Generic routine: you have defined it to work for any class by adding the <T> after the method name. Within the method, you can now use "T" and the compiler with substitute the class type when you call the method.
Effectively, what you could have done is write a separate method for each type:
C#
public static void PrintStackContentsInt(Stack<int> s)
    {
    while(s.Count != 0)
        {
        Console.WriteLine(s.Pop());
        }
    }
public static void PrintStackContentsString(Stack<string> s)
    {
    while(s.Count != 0)
        {
        Console.WriteLine(s.Pop());
        }
    }
And so on: But by using the Generic syntax, you only have to code and maintain a single method, which will work the same for them all.

Anonymous types are more complex, and are probably best left until latter when you start to learn about Linq, but a brief description:
An anonymous type is a class with no name: you do not define it directly with a class statement, instead it's structure is inferred from the data from which it is created. They are frequently used to return the results of a Linq query.
 
Share this answer
 
Comments
sairam.bhat 5-Mar-11 3:40am    
Thank you very much sir
OriginalGriff 5-Mar-11 3:50am    
You're welcome
Sergey Alexandrovich Kryukov 6-Mar-11 0:19am    
Good answer, my 5.
This is getting funny: I tried to re-use my own past Answers and Tips and found 5 my past _big_ posts on the topic (referenced in my Answer), and this is not counting many small ones. Curious to take a look?
--SA
Silju MC 30-Jun-11 5:09am    
good answer
my 5
Anonymous Types Defined
The C# 3.0 specifications describe anonymous types as tuple types automatically inferred and created from object initializers. Before you can fully understand the meaning of this definition, you need to understand the term "object initializer," which is the basis of the anonymous types feature.
An object initializer specifies values from one or more fields or properties of an object. That means you specify a set of properties for an object through a series of assignments, such as {a = 10, b = 20}, and an object is assigned these properties. In other words, an anonymous type is one that previously did not exist and was not defined explicitly in code.
Note: The compiler creates the anonymous type at compile time and not run time.

Just removed the block code.
 
Share this answer
 
v3
Comments
sairam.bhat 5-Mar-11 3:40am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900