0% found this document useful (0 votes)
11 views7 pages

Console Application Examples in C# & VB.NET

The document provides instructions for creating and running console applications in C# and VB.Net, detailing steps for both Command Prompt and Visual Studio IDE. It includes example code for an Employee class that calculates and displays salary details, including basic salary, allowances, and net salary. The examples demonstrate the use of parameterized constructors and methods for calculation and output of employee details.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Console Application Examples in C# & VB.NET

The document provides instructions for creating and running console applications in C# and VB.Net, detailing steps for both Command Prompt and Visual Studio IDE. It includes example code for an Employee class that calculates and displays salary details, including basic salary, allowances, and net salary. The examples demonstrate the use of parameterized constructors and methods for calculation and output of employee details.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Console Application

1. In Command Prompt
a. Go to Command Prompt
b. Write program using Editor(Notepad)
c. Save it (.vb/.cs)
d. Compilation (C:\>vbc [Link] or C:\>vbc [Link]), It will create .Exe file
([Link])
e. Run it (C:\>f1 )
2. IDE
a. Open Visual Studio
b. Click-File->New->Project->Visual Basic/Visual C#->Type is Console
Application
c. Write program, Save it and Run it (F5 or click->Start Debuging Button)

Console Application Examples using C#.Net


Example:-I ([Link])
using System;
using [Link];
using [Link];
using [Link];

namespace ConsoleApplication14
{
class Employee //My Class and My codes
{
//Data members
private int id;
private string name;
private DateTime joinDt;
private double basicSalary;
private double da;
private double hr;
private double grossSalary;
private double it;
private double netSalary;
//Methods
public void calCulate()
{
[Link] = [Link] * 0.3; //30% of basicSalary
[Link] = [Link] * 0.15; //15% of basicSalary
[Link] = [Link] + [Link] + [Link];
[Link] = [Link] * 0.1; //10% of grossSalary
[Link] = [Link] - [Link];
}
public void showEmployeeDetail()
{
[Link]("");
[Link]("Employee Detail");
[Link]("===============");
[Link]("Id :" + [Link]);
[Link]("Name :" + [Link]);
[Link]("Jaoining Date :" + [Link]);
[Link]("Basic Salary :" + [Link]);
[Link]("Daily Allowance(30% of Basic Salary) :" + [Link]);
[Link]("House Rent (15% of Basic Salary) :" + [Link]);
[Link]("Gross Salary(Basic Salary+DA+HR) :" + [Link]);
[Link]("Income Tax (10% of Gross Salary) :" + [Link]);
[Link]("Net Payble Salary (Gross Salay - Income Tax) :" +
[Link]);

}
class Program
{
static void Main(string[] args)
{
Employee e1 = new Employee(); //Create object of Employee
[Link]();
[Link]();
[Link]();

}
}
}
Output

Example:-II ([Link])
using System;
using [Link];
using [Link];
using [Link];

namespace ConsoleApplication14
{
class Employee //My Class and My codes
{
private int id;
private string name;
private DateTime joinDt;
private double basicSalary;
private double da;
private double hr;
private double grossSalary;
private double it;
private double netSalary;
public Employee(int prmId, string prmName, DateTime prmJoinDt, double
prmBasicSalary) //Parameterized Constructor
{
[Link] = prmId;
[Link] = prmName;
[Link] = prmJoinDt;
[Link] = prmBasicSalary;
}
public void calCulate()
{
[Link] = [Link] * 0.3; //30% of basicSalary
[Link] = [Link] * 0.15; //15% of basicSalary
[Link] = [Link] + [Link] + [Link];
[Link] = [Link] * 0.1; //10% of grossSalary
[Link] = [Link] - [Link];
}
public void showEmployeeDetail()
{
[Link]("");
[Link]("Employee Detail");
[Link]("===============");
[Link]("Id :" + [Link]);
[Link]("Name :" + [Link]);
[Link]("Jaoining Date :" + [Link]("dd/MM/yyyy"));
int wYear = [Link] - [Link];
[Link]("Year of Working :" + wYear);
[Link]("Basic Salary :" + [Link]);
[Link]("Daily Allowance(30% of Basic Salary) :" + [Link]);
[Link]("House Rent (15% of Basic Salary) :" + [Link]);
[Link]("Gross Salary(Basic Salary+DA+HR) :" + [Link]);
[Link]("Income Tax (10% of Gross Salary) :" + [Link]);
[Link]("Net Payble Salary (Gross Salay - Income Tax) :" +
[Link]);

}
class Program
{
static void Main(string[] args)
{
DateTime jDt = new DateTime(2000, 7, 29, 0, 0, 0);
Employee e1 = new Employee(1,"Rudra Prasad Dash",jDt,500); //My codes
[Link]();
[Link]();
[Link]();

}
}
}

Output
Console Application Examples using [Link]
Example-I ([Link])
Imports [Link]
Module Module1
Class Employee
'Data Members
Private id As Integer
Private name As String
Private joinDt As DateTime
Private basicSalary As Double
Private da As Double
Private hr As Double
Private grossSalary As Double
Private it As Double
Private netSalary As Double

'Procedure (Do't return any value)


Public Sub calCulate()
[Link] = [Link] * 0.3 '0% of basicSalary
[Link] = [Link] * 0.15 '15% of basicSalary
[Link] = [Link] + [Link] + [Link]
[Link] = [Link] * 0.1 '10% of grossSalary
[Link] = [Link] - [Link]
End Sub

Public Sub showEmployeeDetail()


WriteLine("")
WriteLine("Employee Detail")
WriteLine("===============")
WriteLine("Id :" & [Link])
WriteLine("Name :" & [Link])
WriteLine("Jaoining Date :" & [Link])
WriteLine("Basic Salary :" & [Link])
WriteLine("Daily Allowance(30% of Basic Salary) :" & [Link])
WriteLine("House Rent (15% of Basic Salary) :" & [Link])
WriteLine("Gross Salary(Basic Salary+DA+HR) :" & [Link])
WriteLine("Income Tax (10% of Gross Salary) :" & [Link])
WriteLine("Net Payble Salary (Gross Salay - Income Tax) :" &
[Link])

End Sub
End Class

Sub Main()
Dim e1 As New Employee() 'Create object of Employee
[Link]()
[Link]()
[Link]()
End Sub

End Module
Output

Example-II ([Link])
Imports [Link]
Module Module1
Class Employee
'Data Members
Private id As Integer
Private name As String
Private joinDt As DateTime
Private basicSalary As Double
Private da As Double
Private hr As Double
Private grossSalary As Double
Private it As Double
Private netSalary As Double

Public Sub New(prmId As Integer, prmName As String, prmJoinDt As


DateTime, prmBasicSalary As Double) 'Parameterized Constructor
[Link] = prmId
[Link] = prmName
[Link] = prmJoinDt
[Link] = prmBasicSalary
End Sub

'Procedure (Do't return any value)


Public Sub calCulate()
[Link] = [Link] * 0.3 '0% of basicSalary
[Link] = [Link] * 0.15 '15% of basicSalary
[Link] = [Link] + [Link] + [Link]
[Link] = [Link] * 0.1 '10% of grossSalary
[Link] = [Link] - [Link]
End Sub

Public Sub showEmployeeDetail()


WriteLine("")
WriteLine("Employee Detail")
WriteLine("===============")
WriteLine("Id :" & [Link])
WriteLine("Name :" & [Link])
WriteLine("Jaoining Date :" & [Link]("dd/MM/yyyy"))
Dim wYear As Integer = [Link] - [Link]
WriteLine("Year of Working :" & wYear)
WriteLine("Basic Salary :" & [Link])
WriteLine("Daily Allowance(30% of Basic Salary) :" & [Link])
WriteLine("House Rent (15% of Basic Salary) :" & [Link])
WriteLine("Gross Salary(Basic Salary+DA+HR) :" & [Link])
WriteLine("Income Tax (10% of Gross Salary) :" & [Link])
WriteLine("Net Payble Salary (Gross Salay - Income Tax) :" &
[Link])
End Sub
End Class

Sub Main()
Dim jDt As New DateTime(2000, 7, 29, 0, 0, 0)
Dim e1 As New Employee(1, "Rudra Prasad Dash", jDt, 500) 'Create object
of Employee
[Link]()
[Link]()
[Link]()
End Sub

End Module

Output

Common questions

Powered by AI

To integrate user input for dynamic data processing, the console applications could be modified to prompt users to enter employee details such as ID, name, joining date, and basic salary via 'Console.ReadLine()' for C# and 'ReadLine()' for VB.NET. Parsing and validating this input to ensure it matches expected data types before processing could improve dynamic functionality. This would allow real-time data entry, making the applications more flexible and interactive .

The 'Main' method is critical in both applications as it acts as the entry point for program execution. In both C# and VB.NET, 'Main' instantiates the Employee object, calls the calculation and display methods, and orchestrates the sequence of operations necessary for salary computation. It facilitates a structured flow by invoking defined methods and ensures the program terminates with 'Console.ReadKey()', providing an orderly completion of processes .

The VB.NET console application and the C# application both calculate salary using the same formula for DA, HR, and IT. However, structurally, VB.NET uses different syntax such as 'Sub' instead of methods, and uses '&' for string concatenation instead of '+'. VB.NET also emphasizes procedure-based programming with distinct modules, whereas C# uses classes with methods. Both applications follow the same salary calculation procedure by initializing similar data members and computing the net salary similarly within a class structure .

A potential improvement in both the C# and VB.NET code involves implementing exception handling around input operations or calculations to avoid runtime errors if inputs are incorrect. Another enhancement is the use of properties instead of public methods to access or modify private fields, which would provide a more consistent interface for attribute modification and access control, leading to improved maintainability and adherence to object-oriented design principles .

Using fixed percentage values for DA, HR, and IT limits scalability and flexibility as these components might not accurately reflect the varied financial policies across different organizations or employee agreements. Employees in different roles or positions might require tailored percentages or rates, and the current implementation doesn't allow dynamic adjustment according to external factors such as inflation, tax laws, or specific company policies, potentially necessitating code modification for each unique scenario .

Encapsulation in the example applications is illustrated by the use of private data members in the Employee class, which restricts direct access and modification from outside the class. This ensures control over the data corresponding to each employee, maintaining integrity. Methods like 'calCulate' and 'showEmployeeDetail' provide controlled access to these members, thereby demonstrating encapsulation by exposing functionality while keeping the data safe. Such encapsulation is crucial for making the code modular and maintainable .

In C#, 'showEmployeeDetail' uses the 'Console.WriteLine()' method to display employee details, concatenating text and variables with the '+' operator to format the output. In contrast, VB.NET uses 'WriteLine' with the '&' operator for string concatenation. Both methods iteratively print each attribute of the employee, but VB.NET employs explicit formatting for dates and calculation steps within procedural blocks, adhering to its language-specific constructs .

In the C# console application, parameterized constructors allow for the initialization of employee objects with specific attributes such as ID, name, joining date, and basic salary upon creation. This enhances flexibility by permitting the instantiation of objects with varying attributes without using default values, facilitating the customization of each employee’s data at the time of object creation. This approach simplifies object management and data encapsulation .

Using an IDE like Visual Studio offers several benefits over a command-line interface for developing console applications, including syntax highlighting, debugging tools, and project management features. An IDE provides visual aids to manage code complexity, reduces manual errors with auto-complete functions, and offers an integrated debugger for identifying and fixing errors faster. It also facilitates version control and better organization through project templates, which can speed up development and improve code quality .

The C# console application calculates the net salary of an employee by first determining the daily allowance (DA) as 30% of the employee's basic salary and the house rent (HR) as 15% of the basic salary. It then sums up the basic salary, DA, and HR to compute the gross salary. An income tax (IT) of 10% is deducted from the gross salary to arrive at the net salary. The net salary is calculated using the formula: Net Salary = Gross Salary - Income Tax .

You might also like