Hi everyone,
I recently started to learn C# and picked up the excellent Head First C# book. One thing I noticed that the book teaches is that the keyword NEW is used to created an instance of a class. I would like to know if you need to use the NEW keyword or if you can't simply create a class, say Example, and then just call Example EX1, instead of doing Example EX1=new Example?
Thanks for any clarification on this
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Oct 11, 2008, 9:40 PM EDT
You have to declare the method Static in Order to use it direct without making Class Instance
Here I have Created Employee Class and in the Employee Class Having Method "printEmployeeName()" and declared it as Static method
Now i call this method from main Function Without making Instance of class
public class Test
{
public static void Main()
{
Employee.printEmployeeName();
Console.ReadLine();
}
}
class Employee
{
public static void printEmployeeName()
{
Console.Write("Emp Name is Kirtan");
}
}
Thanks ,
Hope It Will be helpful to you :-)
Happy Coding :-)
Daniel Innala Ahlmark25 reputationPosted Oct 12, 2008, 10:28 AM EDT
or shorter:
Example ex = new Example();
Don9 reputationPosted Oct 13, 2008, 10:05 AM EDT
As I remember from C++, you could just create a class and then to create an instance of that class, if the class was called student, wouldn't you just do this: student Jake, student Tom, etc?