|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
6424
|
|
Downloads :
|
17
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
There are many occasions when you need to create an empty project and add classes and methods late to the project. There are many tutorial, which will show you how to create Windows or Web applications, but you rarely see a tutorial building an empty project.
This tutorial guides you towards your first C# Empty project step by step. After that I'll show you how to add a class to the project and few members to the class and call this class members from the main method.
Step 1: Create an Empty Project
Select File->New->Project->Visual C# Projects->Empty Project. Select your project name and appropriate directory using Browse button. See Figure 1.

Figure 1.
When you create an empty project, there is no class available in the project.
Step 2: Adding an Empty Class
Right click on the project in Solution Explorer and click Add->Add Class. See Figure 2.

Figure 2.
Add Class option launches Add New Item dialog, where you can select from many items. I select Class option as you can see from Figure 3. Write name of your class and click Open. I put my class name as mcbClass. Click Open here.

Now you have added a class to your project. Visual Studio create a skeleton of the class for you. It looks like here:
using System; |namespace FirstEmptyApp { /// <summary> /// Summary description for mcbClass. /// </summary> public class mcbClass { public mcbClass() { // // TODO: Add constructor logic here // } } }
Step 3: Add Functionality to your Class
Now it time to add functionality to your class. I add few methods to this class. As you can see from the following listing, I add Add, Multiply, Subtract, and Sqaure methods to the class.
public class mcbClass { public mcbClass() { // // TODO: Add constructor logic here // } // Square function public int Square(int num) { return num*num; } // Add two integers and returns the sum public int Add(int num1, int num2 ) { return num1 + num2; } // Add two integers and returns the sum public double Add(double num1, double num2 ) { return num1 + num2; } // Multiply two integers and retuns the result public int Multiply(int num1, int num2 ) { return num1 * num2; } // Subtracts small number from big number public int Subtract(int num1, int num2 ) { if ( num1 > num2 ) { return num1 - num2; } return num2 - num1; } }
Now your class is ready. Next step is to add a Main method to the project. The Main method executes when you start an application, means it's the entry point of an application. See BOLD part in the following listing.
I add Main method to the same namespace just after mcbClass. Now the entire code looks like following listing.
using System; namespace FirstEmptyApp { /// <summary> /// Summary description for mcbClass. /// </summary> public class mcbClass { public mcbClass() { // // TODO: Add constructor logic here // } // Square function public int Square(int num) { return num*num; } // Add two integers and returns the sum public int Add(int num1, int num2 ) { return num1 + num2; } // Add two integers and returns the sum public double Add(double num1, double num2 ) { return num1 + num2; } // Multiply two integers and retuns the result public int Multiply(int num1, int num2 ) { return num1 * num2; } // Subtracts small number from big number public int Subtract(int num1, int num2 ) { if ( num1 > num2 ) { return num1 - num2; } return num2 - num1; } } // Call mcClass from Main public class Class1 { static void Main() { mcbClass sq = new mcbClass(); Console.WriteLine( sq.Square(8).ToString()); Console.WriteLine( sq.Add(8.3, 9.24).ToString()); Console.WriteLine( sq.Multiply(5,8).ToString()); Console.WriteLine( sq.Subtract(22, 42).ToString()); } } }
Step 4: Build and Run the Project
Now build and run project. You can use Ctrl+F5 or Start Without Debugging menu option from the Debug menu. See Figure 4.
Figure 4.
The output of application looks like Figure 5.

|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|