Introduction to Libraries
Introduction to Libraries
Custom Libraries
Introduction
A library is a program that contains classes and/or other resources that some other programs can use. A library can be used to assist a computer language to perform one or various types of operations such as arithmetic calculations, scientific manipulations, medical tests, simulations, chemistry, artificial intelligence, drawing, etc. Many languages use various types of libraries to assist them. Some languages have or include their own libraries. Some libraries are installed along with a language. Some other libraries must be acquired (and/or purchased) and installed separately.
To assist your project with some types of operations, you can create a library and use it in one or more of your programs. You can even create a commercial library and distribute or sell it.
There are various techniques to create a library but it is primarily created as a computer application. A library is not an executable; as a result, it doesn't include the Main() method. A library usually has the extension .dll (other types of libraries in Microsoft Windows can have the extension .lib or another extension).
Practical Learning: Introducing Custom Libraries
A Library Contains Classes
A library can be made of a single file or as many files as necessary. A file that is part of a library can contain one or more classes. Each class should implement a behavior that can eventually be useful and accessible to other classes. The classes in a library are created exactly like those of other programs.
Practical Learning: Starting a Library
public class Conversion { // This is a common property that all methods will use for conversion public double Number { get; set; } public void InchesToCentimeters() { double result = Number * 2.54; System.Console.Write(result); } public void CentimetersToInches() { double result = Number * 0.393701; System.Console.Write(result); } public void FeetToMeters() { System.Console.Write(Number * 0.3048); } public void MetersToFeet() { System.Console.Write(Number * 3.2808); } } namespace SquaredValues { public class Areas { public double Number { get; set; } public void SquaredInchesToCentimeters() { double result = Number * 6.4516; System.Console.Write(result); } public void SquaredCentimetersToInches() { double result = Number * .155; System.Console.Write(result); } public void AcreToHectare() { System.Console.Write(Number * .4047); } } } namespace CubedValues { public class Volumes { public double Number { get; set; } public void LitreToGallons() { double result = Number * .2642; System.Console.Write(result); } } }
Creating a Library
Microsoft Visual Studio makes it convenient to create a library, from scratch or as a project. You have various options and for various goals, depending on the type of library you want and for the type of project that will use the library. To create a library project, first display the Create a New Project dialog box. In the list of projects templates, click:
Then click Next. Accept or change the name of the project. If you had started from an empty project or another type (in which case you would make sure to remove the Main() method), display the project Properties window and set the Output Type as Class Library.
Practical Learning: Creating a Library
Building a Library
When you are creating a library and not an executable, you must build the project to create the actual library. To do that:
Practical Learning: Building a Library
Using a Custom Library
After building the project, you can use it. You can use it in the same project where the library was built or you can use it in another project. If you are working in Microsoft Visual Studio, you can start by creating a new project. To use the library, you would have to reference it. To do this:
In both cases, the Reference Manager dialog box would come up. You can click the Browse tab, locate the folder where the library resides and select it:
After selecting the library, you can click Add.
In the Reference Manager dialog box, click OK. You can then use the classes and members of the library.
Practical Learning: Using a Custom Library
public class Algebra { static void Main() { Conversion converter = new Conversion(); converter.Number = 124.475; System.Console.WriteLine("Metric Conversion"); System.Console.WriteLine("====================================="); System.Console.WriteLine("Inches to Centimeters"); System.Console.Write("Number: "); System.Console.Write(converter.Number); System.Console.WriteLine(" Inches"); System.Console.Write("Result: "); converter.InchesToCentimeters(); System.Console.WriteLine(" Centimeters"); System.Console.WriteLine("-------------------------------------"); System.Console.WriteLine("Centimeters to Inches"); System.Console.Write("Number: "); System.Console.Write(converter.Number); System.Console.WriteLine(" Centimeters"); System.Console.Write("Result: "); converter.CentimetersToInches(); System.Console.WriteLine(" Inches"); System.Console.WriteLine("-------------------------------------"); System.Console.WriteLine("Meters to Feet"); System.Console.Write("Number: "); System.Console.Write(converter.Number); System.Console.WriteLine(" Meters"); System.Console.Write("Result: "); converter.MetersToFeet(); System.Console.WriteLine(" Feet"); System.Console.WriteLine("====================================="); SquaredValues.Areas surfaces = new SquaredValues.Areas(); surfaces.Number = converter.Number; System.Console.WriteLine("Squared Inches to Squared Centimeters"); System.Console.Write("Number: "); System.Console.Write(surfaces.Number); System.Console.WriteLine(" Squared Inches"); System.Console.Write("Result: "); surfaces.SquaredInchesToCentimeters(); System.Console.WriteLine(" Squared Centimeters"); System.Console.WriteLine("-------------------------------------"); System.Console.WriteLine("Squared Centimeters to Squared Inches"); System.Console.Write("Number: "); System.Console.Write(surfaces.Number); System.Console.WriteLine(" Squared Centimeters"); System.Console.Write("Result: "); surfaces.SquaredCentimetersToInches(); System.Console.WriteLine(" Squared Inches"); System.Console.WriteLine("-------------------------------------"); System.Console.WriteLine("Acre to Hectare"); System.Console.Write("Number: "); System.Console.Write(surfaces.Number); System.Console.WriteLine(" Acres"); System.Console.Write("Result: "); surfaces.AcreToHectare(); System.Console.WriteLine(" Hectares"); System.Console.WriteLine("====================================="); CubedValues.Volumes vols = new CubedValues.Volumes(); vols.Number = converter.Number; System.Console.WriteLine("Litre to Gallons"); System.Console.Write("Number: "); System.Console.Write(vols.Number); System.Console.WriteLine(" Litres"); System.Console.Write("Result: "); vols.LitreToGallons(); System.Console.WriteLine(" Gallons"); System.Console.WriteLine("====================================="); } }
Metric Conversion ===================================== Inches to Centimeters Number: 124.475 Inches Result: 316.1665 Centimeters ------------------------------------- Centimeters to Inches Number: 124.475 Centimeters Result: 49.005931975 Inches ------------------------------------- Meters to Feet Number: 124.475 Meters Result: 408.37758 Feet ===================================== Squared Inches to Squared Centimeters Number: 124.475 Squared Inches Result: 803.06291 Squared Centimeters ------------------------------------- Squared Centimeters to Squared Inches Number: 124.475 Squared Centimeters Result: 19.293625 Squared Inches ------------------------------------- Acre to Hectare Number: 124.475 Acres Result: 50.3750325 Hectares ===================================== Litre to Gallons Number: 124.475 Litres Result: 32.886295 Gallons ===================================== Press any key to continue . . .
class OrderProcessing { static void Main() { double priceOneShirt = 1.15; double priceAPairOfPants = 1.85; double priceOneDress = 3.35; double discountRate = 0.10; // 20% double taxRate = 5.75; // 5.75% string customerName = "James Burreck", homePhone = "(202) 301-7030"; int numberOfShirts = 5, numberOfPants = 2, numberOfDresses = 3; int totalNumberOfItems; double subTotalShirts, subTotalPants, subTotalDresses; double discountAmount, totalOrder, netPrice, taxAmount, salesTotal; double amountTended, difference; int orderMonth = 3, orderDay = 15, orderYear = 2020; totalNumberOfItems = numberOfShirts + numberOfPants + numberOfDresses; subTotalShirts = priceOneShirt * numberOfShirts; subTotalPants = priceAPairOfPants * numberOfPants; subTotalDresses = numberOfDresses * priceOneDress; totalOrder = subTotalShirts + subTotalPants + subTotalDresses; discountAmount = totalOrder * discountRate; netPrice = totalOrder - discountAmount; taxAmount = totalOrder * taxRate / 100; salesTotal = netPrice + taxAmount; amountTended = 50; difference = amountTended - salesTotal; System.Console.WriteLine("-/- Georgetown Cleaning Services -/-"); System.Console.WriteLine("===================================="); System.Console.Write("Customer: "); System.Console.WriteLine(customerName); System.Console.Write("Home Phone: "); System.Console.WriteLine(homePhone); System.Console.Write("Order Date: "); System.Console.Write(orderMonth); System.Console.Write("/"); System.Console.Write(orderDay); System.Console.Write("/"); System.Console.WriteLine(orderYear); System.Console.WriteLine("------------------------------------"); System.Console.WriteLine("Item Type Qty Unit/Price Sub-Total"); System.Console.WriteLine("------------------------------------"); System.Console.Write("Shirts "); System.Console.Write(numberOfShirts); System.Console.Write(" "); System.Console.Write(priceOneShirt); System.Console.Write(" "); System.Console.WriteLine(subTotalShirts); System.Console.Write("Pants "); System.Console.Write(numberOfPants); System.Console.Write(" "); System.Console.Write(priceAPairOfPants); System.Console.Write(" "); System.Console.WriteLine(subTotalPants); System.Console.Write("Dresses "); System.Console.Write(numberOfDresses); System.Console.Write(" "); System.Console.Write(priceOneDress); System.Console.Write(" "); System.Console.WriteLine(subTotalDresses); System.Console.WriteLine("------------------------------------"); System.Console.Write("Number of Items: "); System.Console.WriteLine(totalNumberOfItems); System.Console.Write("Total Order: "); System.Console.WriteLine(totalOrder); System.Console.Write("Discount Rate: "); System.Console.Write(discountRate * 100); System.Console.WriteLine('%'); System.Console.Write("Discount Amount: "); System.Console.WriteLine(discountAmount); System.Console.Write("After Discount: "); System.Console.WriteLine(netPrice); System.Console.Write("Tax Rate: "); System.Console.Write(taxRate); System.Console.WriteLine('%'); System.Console.Write("Tax Amount: "); System.Console.WriteLine(taxAmount); System.Console.Write("Net Price: "); System.Console.WriteLine(salesTotal); System.Console.WriteLine("===================================="); System.Console.Write("Amount Tended: "); System.Console.WriteLine(amountTended); System.Console.Write("Difference: "); System.Console.WriteLine(difference); System.Console.WriteLine("===================================="); } }
-/- Georgetown Cleaning Services -/- ==================================== Customer: James Burreck Home Phone: (202) 301-7030 Order Date: 3/15/2020 ------------------------------------ Item Type Qty Unit/Price Sub-Total ------------------------------------ Shirts 5 1.15 5.75 Pants 2 1.85 3.7 Dresses 3 3.35 10.05 ------------------------------------ Number of Items: 10 Total Order: 19.5 Discount Rate: 10% Discount Amount: 1.95 After Discount: 17.55 Tax Rate: 5.75% Tax Amount: 1.12125 Net Price: 18.67125 ==================================== Amount Tended: 50 Difference: 31.32875 ==================================== Press any key to continue . . .
Introduction to Built-In Libraries
Introduction the .NET Framework
To assist programmers with their duties, Microsoft created a library named the .NET Framework. It is a huge library made of various classes aimed at different scenarios. When you are programming in C#, the primary library you must use is the .NET Framework. If the .NET Framework does not provide a functionality you are looking for, you can create you own library and use it in your application(s). Still, as we will see throughout our lessons, before creating a library, make sure the .NET Framework doesn't have the functionality you are looking for.
Using the .NET Framework
The .NET Framework contains a tremendous number of classes. The classes are created in various namespaces. To use a class, first identify the class you want, then find out in which namespace the class exists. As seen in our introduction to namespaces, to indicate that you want to use a class that exists in a namespace, in the top section of the document, write the using keyword followed by the name of the namespace.
The .NET Framework library is central to C#. That library includes hundreds or thousands of namespaces and thousands of classes. One way or another, all those namespaces and classes are available to C#. This means that you have tremendous choices when programming in C#. To use a class of the .NET Framework in your C# application, you need to know what class contains the behavior you are looking for.
The Object Browser
The .NET Framework is a very rich, powerful, and expanded library. Its primary power is in its large set of classes. To organize these classes, the .NET Framework provides many namespaces. Each namespace is used to provide a specific set of classes.
To help you examine the namespaces of the .NET Framework, Microsoft Visual Studio provides the Object Browser. To access it, on the main menu, click View -> Object Browser. The Object is made of three frames.
The left frame shows a list of libraries. To show the content of a library, you can expand it, which is done by clicking its left triangular button. To see the list of namespaces created in a library, expand its library:
To see the list of classes that belong to the same namespace, expand that namespace. Here is an example:
Introduction the System Namespace
The most fundamental namespace of the .NET Framework is named System. It contains the primary data types you use in an application. It also contains the classes used in a regular application.
You will need the System namespace for almost every application you are creating. The primary way to use the System namespace in an application is to type the using keyword followed by System;. Here is an example:
using System;
class Program
{
static void Main()
{
}
}
Introduction to the Console
Introduction
One of the classes created in the System namespace is named Console. This is the most fundamental class used for an application that displays some text in a black window. To use the Console class, after typing using System; in the top section of the document, in the body of a class, you can type Console. You can also use System.Console.
The System.Console class is created in a library named System.Console.dll. When you create a C# application in Microsoft Visual Studio, the System.Console.dll library is automatically included so you don't have to add it to your project.
Writing to the Console
The System.Console class is equipped with various methods. One method is named Write. In its parentheses, you can include the value you want to display. As mentionned already, in a document where you want to call a method of the Console class, you can type Console. followed by the name of the method and its parentheses. Here is an example:
using System; class Exercise { static void Main() { Console.Write("Welcome to the World of C# Programming!"); } }
Creating a New Line
In the parentheses of the Console.Write() method, you can include the name of a variable. The Console.Write() method displays its value and keeps the caret on the same line. Another method of the Console class is named WriteLine. This method takes 0 or more values as arguments. When the Console.WriteLine() method has displayed nothing or a value, it continues with the caret on the next line.
Practical Learning: Using the Console Class
using System;
using System;
class OrderProcessing
{
static void Main()
{
double priceOneShirt = 1.15;
double priceAPairOfPants = 1.85;
double priceOneDress = 3.35;
double discountRate = 0.10; // 10%
double taxRate = 5.75; // 5.75%
string customerName = "James Burreck",
homePhone = "(202) 301-7030";
int numberOfShirts = 5,
numberOfPants = 2,
numberOfDresses = 3;
int totalNumberOfItems;
double subTotalShirts, subTotalPants, subTotalDresses;
double discountAmount, totalOrder, netPrice, taxAmount, salesTotal;
double amountTended, difference;
int orderMonth = 3, orderDay = 15, orderYear = 2020;
totalNumberOfItems = numberOfShirts + numberOfPants + numberOfDresses;
subTotalShirts = priceOneShirt * numberOfShirts;
subTotalPants = priceAPairOfPants * numberOfPants;
subTotalDresses = numberOfDresses * priceOneDress;
totalOrder = subTotalShirts + subTotalPants + subTotalDresses;
discountAmount = totalOrder * discountRate;
netPrice = totalOrder - discountAmount;
taxAmount = totalOrder * taxRate / 100;
salesTotal = netPrice + taxAmount;
amountTended = 50;
difference = amountTended - salesTotal;
Console.WriteLine("-/- Georgetown Cleaning Services -/-");
Console.WriteLine("====================================");
Console.Write("Customer: ");
Console.WriteLine(customerName);
Console.Write("Home Phone: ");
Console.WriteLine(homePhone);
Console.Write("Order Date: ");
Console.Write(orderMonth);
Console.Write("/");
Console.Write(orderDay);
Console.Write("/");
Console.WriteLine(orderYear);
Console.WriteLine("------------------------------------");
Console.WriteLine("Item Type Qty Unit/Price Sub-Total");
Console.WriteLine("------------------------------------");
Console.Write("Shirts ");
Console.Write(numberOfShirts);
Console.Write(" ");
Console.Write(priceOneShirt);
Console.Write(" ");
Console.WriteLine(subTotalShirts);
Console.Write("Pants ");
Console.Write(numberOfPants);
Console.Write(" ");
Console.Write(priceAPairOfPants);
Console.Write(" ");
Console.WriteLine(subTotalPants);
Console.Write("Dresses ");
Console.Write(numberOfDresses);
Console.Write(" ");
Console.Write(priceOneDress);
Console.Write(" ");
Console.WriteLine(subTotalDresses);
Console.WriteLine("------------------------------------");
Console.Write("Number of Items: ");
Console.WriteLine(totalNumberOfItems);
Console.Write("Total Order: ");
Console.WriteLine(totalOrder);
Console.Write("Discount Rate: ");
Console.Write(discountRate * 100);
Console.WriteLine('%');
Console.Write("Discount Amount: ");
Console.WriteLine(discountAmount);
Console.Write("After Discount: ");
Console.WriteLine(netPrice);
Console.Write("Tax Rate: ");
Console.Write(taxRate);
Console.WriteLine('%');
Console.Write("Tax Amount: ");
Console.WriteLine(taxAmount);
Console.Write("Net Price: ");
Console.WriteLine(salesTotal);
Console.WriteLine("====================================");
Console.Write("Amount Tended: ");
Console.WriteLine(amountTended);
Console.Write("Difference: ");
Console.WriteLine(difference);
Console.WriteLine("====================================");
}
}
Starting a .NET Core Console Project
So far, we were creating empty projects to learn how to work from scratch. So far, we were creating only console applications. In reality, Microsoft Visual Studio provides various templates you can use to create a console application.
One of the sub-libraries in the .NET Framework is called .NET Core. It supports console applications. To create a console application that supports the .NET Core, display the Create a New Project dialog box. In the list of projects templates, click Console App (.NET Core):
Practical Learning: Creating a .NET Core Console Project
using System; namespace GeorgetownDryCleaningServices4 { class Program { static void Main() { double priceOneShirt = 1.25; double priceAPairOfPants = 1.75; double priceOneDress = 3.25; double discountRate = 0.15; // 10% double taxRate = 5.75; // 5.75% string customerName = "Erika Wendels", homePhone = "(410) 846-3805"; int numberOfShirts = 3, numberOfPants = 6, numberOfDresses = 5; int totalNumberOfItems; double subTotalShirts, subTotalPants, subTotalDresses; double discountAmount, totalOrder, netPrice, taxAmount, salesTotal; double amountTended, difference; int orderMonth = 6, orderDay = 3, orderYear = 2020; totalNumberOfItems = numberOfShirts + numberOfPants + numberOfDresses; subTotalShirts = priceOneShirt * numberOfShirts; subTotalPants = priceAPairOfPants * numberOfPants; subTotalDresses = numberOfDresses * priceOneDress; totalOrder = subTotalShirts + subTotalPants + subTotalDresses; discountAmount = totalOrder * discountRate; netPrice = totalOrder - discountAmount; taxAmount = totalOrder * taxRate / 100; salesTotal = netPrice + taxAmount; amountTended = 30; difference = amountTended - salesTotal; Console.WriteLine("-/- Georgetown Cleaning Services -/-"); Console.WriteLine("===================================="); Console.Write("Customer: "); Console.WriteLine(customerName); Console.Write("Home Phone: "); Console.WriteLine(homePhone); Console.Write("Order Date: "); Console.Write(orderMonth); Console.Write("/"); Console.Write(orderDay); Console.Write("/"); Console.WriteLine(orderYear); Console.WriteLine("------------------------------------"); Console.WriteLine("Item Type Qty Unit/Price Sub-Total"); Console.WriteLine("------------------------------------"); Console.Write("Shirts "); Console.Write(numberOfShirts); Console.Write(" "); Console.Write(priceOneShirt); Console.Write(" "); Console.WriteLine(subTotalShirts); Console.Write("Pants "); Console.Write(numberOfPants); Console.Write(" "); Console.Write(priceAPairOfPants); Console.Write(" "); Console.WriteLine(subTotalPants); Console.Write("Dresses "); Console.Write(numberOfDresses); Console.Write(" "); Console.Write(priceOneDress); Console.Write(" "); Console.WriteLine(subTotalDresses); Console.WriteLine("------------------------------------"); Console.Write("Number of Items: "); Console.WriteLine(totalNumberOfItems); Console.Write("Total Order: "); Console.WriteLine(totalOrder); Console.Write("Discount Rate: "); Console.Write(discountRate * 100); Console.WriteLine("%"); Console.Write("Discount Amount: "); Console.WriteLine(discountAmount); Console.Write("After Discount: "); Console.WriteLine(netPrice); Console.Write("Tax Rate: "); Console.Write(taxRate); Console.WriteLine("%"); Console.Write("Tax Amount: "); Console.WriteLine(taxAmount); Console.Write("Net Price: "); Console.WriteLine(salesTotal); Console.WriteLine("===================================="); Console.Write("Amount Tended: "); Console.WriteLine(amountTended); Console.Write("Difference: "); Console.WriteLine(difference); Console.WriteLine("===================================="); } } }
-/- Georgetown Cleaning Services -/- ==================================== Customer: Erika Wendels Home Phone: (410) 846-3805 Order Date: 6/3/2020 ------------------------------------ Item Type Qty Unit/Price Sub-Total ------------------------------------ Shirts 3 1.25 3.75 Pants 6 1.75 10.5 Dresses 5 3.25 16.25 ------------------------------------ Number of Items: 14 Total Order: 30.5 Discount Rate: 15% Discount Amount: 4.575 After Discount: 25.925 Tax Rate: 5.75% Tax Amount: 1.75375 Net Price: 27.67875 ==================================== Amount Tended: 30 Difference: 2.321249999999999 ==================================== C:\Users\pkatts\Source\Repos\CSharp\GeorgetownDryCleaningServices4\GeorgetownDry CleaningServices4\bin\Debug\netcoreapp3.1\GeorgetownDryCleaningServices4.exe (pr ocess 6972) exited with code 0. Press any key to close this window . . .
Starting a .NET Framework Console Project
The primary .NET Framework itself supports the console applications. To create a console application that benefits from the .NET Framework, display the Create a New Project dialog box. In the list of projects templates, click Console App (.NET Framework).
Practical Learning: Creating a .NET Core Console Project
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GeorgetownDryCleaningServices5 { class Program { static void Main() { double priceOneShirt = 2; double priceAPairOfPants = 1.75; double priceOneDress = 3.5; double discountRate = 0.1225; // 12.25% double taxRate = 6.15; // 6.15% string customerName = "John Krantz", homePhone = "(703) 279-4753"; int numberOfShirts = 8, numberOfPants = 5, numberOfDresses = 3; int totalNumberOfItems; double subTotalShirts, subTotalPants, subTotalDresses; double discountAmount, totalOrder, netPrice, taxAmount, salesTotal; double amountTended, difference; int orderMonth = 10, orderDay = 14, orderYear = 2020; totalNumberOfItems = numberOfShirts + numberOfPants + numberOfDresses; subTotalShirts = priceOneShirt * numberOfShirts; subTotalPants = priceAPairOfPants * numberOfPants; subTotalDresses = numberOfDresses * priceOneDress; totalOrder = subTotalShirts + subTotalPants + subTotalDresses; discountAmount = totalOrder * discountRate; netPrice = totalOrder - discountAmount; taxAmount = totalOrder * taxRate / 100; salesTotal = netPrice + taxAmount; amountTended = 27; difference = amountTended - salesTotal; Console.WriteLine("-/- Georgetown Cleaning Services -/-"); Console.WriteLine("===================================="); Console.Write("Customer: "); Console.WriteLine(customerName); Console.Write("Home Phone: "); Console.WriteLine(homePhone); Console.Write("Order Date: "); Console.Write(orderMonth); Console.Write("/"); Console.Write(orderDay); Console.Write("/"); Console.WriteLine(orderYear); Console.WriteLine("------------------------------------"); Console.WriteLine("Item Type Qty Unit/Price Sub-Total"); Console.WriteLine("------------------------------------"); Console.Write("Shirts "); Console.Write(numberOfShirts); Console.Write(" "); Console.Write(priceOneShirt); Console.Write(" "); Console.WriteLine(subTotalShirts); Console.Write("Pants "); Console.Write(numberOfPants); Console.Write(" "); Console.Write(priceAPairOfPants); Console.Write(" "); Console.WriteLine(subTotalPants); Console.Write("Dresses "); Console.Write(numberOfDresses); Console.Write(" "); Console.Write(priceOneDress); Console.Write(" "); Console.WriteLine(subTotalDresses); Console.WriteLine("------------------------------------"); Console.Write("Number of Items: "); Console.WriteLine(totalNumberOfItems); Console.Write("Total Order: "); Console.WriteLine(totalOrder); Console.Write("Discount Rate: "); Console.Write(discountRate * 100); Console.WriteLine("%"); Console.Write("Discount Amount: "); Console.WriteLine(discountAmount); Console.Write("After Discount: "); Console.WriteLine(netPrice); Console.Write("Tax Rate: "); Console.Write(taxRate); Console.WriteLine("%"); Console.Write("Tax Amount: "); Console.WriteLine(taxAmount); Console.Write("Net Price: "); Console.WriteLine(salesTotal); Console.WriteLine("===================================="); Console.Write("Amount Tended: "); Console.WriteLine(amountTended); Console.Write("Difference: "); Console.WriteLine(difference); Console.WriteLine("===================================="); } } }
-/- Georgetown Cleaning Services -/- ==================================== Customer: Erika Wendels Home Phone: (410) 846-3805 Order Date: 10/14/2020 ------------------------------------ Item Type Qty Unit/Price Sub-Total ------------------------------------ Shirts 8 2 16 Pants 5 1.75 8.75 Dresses 3 3.5 10.5 ------------------------------------ Number of Items: 16 Total Order: 35.25 Discount Rate: 12.25% Discount Amount: 4.318125 After Discount: 30.931875 Tax Rate: 6.15% Tax Amount: 2.167875 Net Price: 33.09975 ==================================== Amount Tended: 40 Difference: 6.90025 ==================================== Press any key to continue . . .
Removing Unused Usings
Whenever you create a project in Microsoft Visual Studio using one of the non-Empty templates, the studio writes some primary code in the files that it creates. In the same way, the studio adds some using lines in the files. In most cases, you will not use some of the namespaces that are added, or you will know that some of the namespaces are not necessary for your file. As a matter of fact, by analyzing the content of each document, Microsoft Visual Studio knows when a namespace is not used in a document. The text of a namesoace that is not used is blurred as compared to the names of the namespaces that are used.
When a namespace is not used in your code, you should remove that namespace and its line. You can manually delete the line of a namespace but you could make a mistake when deleting such a line. Fortunately, Microsoft Visual Studio can assist you. To remove all the unused namespaces, right-click the section of usings and click Remove and Sort Usings.
Practical Learning: Removing Unused Usings
Introduction to the C# Library
Introduction
The C# language has a small library made of just a few classes you probably will hardly use. The library is named Microsoft.CSharp.dll. If you create a console application, Microsoft Visual Studio automatically adds the Microsoft.CSharp.dll library to your project. Otherwise, if you create an empty project but you want to use this library, you can easily add it using the Reference Manager dialog box. To add it:
A data type communicates to the compiler how much memory it should reserve for the variable. In some scenarios, you would not know how much space is appropriate, until you need to use the variable. In other words, you would not worry the compiler with the variable's logistics until the operation that needs the variable that accesses it. Such a variable is referred to as dynamic.
A Dynamic Variable
To let you declare dynamic variables, the C# language provides a keyword named dynamic. It is used as a data type. Here is an example of declaring a dynamic variable:
class Exercise
{
static void Main()
{
dynamic value;
}
}
You can initialize the variable when declaring it. The formula to follow is:
class Exercise
{
static void Main()
{
dynamic variable-name = desired-value;
}
}
You don't have to initialize a dynamic variable when declaring it. Still, before using it, at one time or another, you must assign a value to it. When you assign a value to a dynamic variable, the compiler doesn't check how much space would suit the variable. A dynamic variable is mostly used if you are creating a project that would communicate with an external application and it would use values coming from that application. An example is a Microsoft Word document or a Microsoft Excel spreadsheet developed using the C# language (or from Microsoft Visual Studio) that imports an external library.
Using Microsoft Visual Basic in C#
Introduction
Besides C#, Visual Basic is another language that supports the .NET Framework. The Visual Basic language includes its own library, which is huge and wonderfully made. The Visual Basic library contains classes that provide the functionality of that language to make it available to other languages. You can use those classes in your C# code.
To use a Visual Basic library in your C# application, you must add a reference to it. The Visual Basic library is available in the Microsoft.VisualBasic.dll library.
Practical Learning: Using the Visual Basic Library
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GeorgetownDryCleaningServices5 { class Program { static void Main() { double priceOneShirt = 2; double priceAPairOfPants = 1.75; double priceOneDress = 3.5; double discountRate = 0.1225; // 12.25% double taxRate = 6.15; // 6.15% string customerName = "John Krantz", homePhone = "(703) 279-4753"; int numberOfShirts = 8, numberOfPants = 5, numberOfDresses = 3; int totalNumberOfItems; double subTotalShirts, subTotalPants, subTotalDresses; double discountAmount, totalOrder, netPrice, taxAmount, salesTotal; double amountTended, difference; int orderMonth = 10, orderDay = 14, orderYear = 2020; totalNumberOfItems = numberOfShirts + numberOfPants + numberOfDresses; subTotalShirts = priceOneShirt * numberOfShirts; subTotalPants = priceAPairOfPants * numberOfPants; subTotalDresses = numberOfDresses * priceOneDress; totalOrder = subTotalShirts + subTotalPants + subTotalDresses; discountAmount = totalOrder * discountRate; netPrice = totalOrder - discountAmount; taxAmount = totalOrder * taxRate / 100; salesTotal = netPrice + taxAmount; amountTended = 40; difference = amountTended - salesTotal; Console.WriteLine("-/- Georgetown Cleaning Services -/-"); Console.WriteLine("===================================="); Console.Write("Customer: "); Console.WriteLine(customerName); Console.Write("Home Phone: "); Console.WriteLine(homePhone); Console.Write("Order Date: "); Console.Write(orderMonth); Console.Write("/"); Console.Write(orderDay); Console.Write("/"); Console.WriteLine(orderYear); Console.WriteLine("------------------------------------"); Console.WriteLine("Item Type Qty Unit/Price Sub-Total"); Console.WriteLine("------------------------------------"); Console.Write("Shirts "); Console.Write(numberOfShirts); Console.Write(" "); Console.Write(Microsoft.VisualBasic.Strings.FormatNumber(priceOneShirt)); Console.Write(" "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(subTotalShirts)); Console.Write("Pants "); Console.Write(numberOfPants); Console.Write(" "); Console.Write(Microsoft.VisualBasic.Strings.FormatNumber(priceAPairOfPants)); Console.Write(" "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(subTotalPants)); Console.Write("Dresses "); Console.Write(numberOfDresses); Console.Write(" "); Console.Write(Microsoft.VisualBasic.Strings.FormatNumber(priceOneDress)); Console.Write(" "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(subTotalDresses)); Console.WriteLine("------------------------------------"); Console.Write("Number of Items: "); Console.WriteLine(totalNumberOfItems); Console.Write("Total Order: "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(totalOrder)); Console.Write("Discount Rate: "); Console.Write(discountRate * 100); Console.WriteLine("%"); Console.Write("Discount Amount: "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(discountAmount)); Console.Write("After Discount: "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(netPrice)); Console.Write("Tax Rate: "); Console.Write(taxRate); Console.WriteLine("%"); Console.Write("Tax Amount: "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(taxAmount)); Console.Write("Net Price: "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(salesTotal)); Console.WriteLine("===================================="); Console.Write("Amount Tended: "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(amountTended)); Console.Write("Difference: "); Console.WriteLine(Microsoft.VisualBasic.Strings.FormatNumber(difference)); Console.WriteLine("===================================="); } } }
-/- Georgetown Cleaning Services -/- ==================================== Customer: John Krantz Home Phone: (703) 279-4753 Order Date: 10/14/2020 ------------------------------------ Item Type Qty Unit/Price Sub-Total ------------------------------------ Shirts 8 2.00 16.00 Pants 5 1.75 8.75 Dresses 3 3.50 10.50 ------------------------------------ Number of Items: 16 Total Order: 35.25 Discount Rate: 12.25% Discount Amount: 4.32 After Discount: 30.93 Tax Rate: 6.15% Tax Amount: 2.17 Net Price: 33.10 ==================================== Amount Tended: 40.00 Difference: 6.90 ==================================== Press any key to continue . . .
My Visual Basic Namespace
The Visual Basic provides many of its operations as services. Each service is created as a (Visual Basic) class. To make the services/classes available, the Visual Basic language provides a namespace named My. Fortunately, the language makes its services available to other .NET languages.
Before using a Visual Basic service, you must add a reference to the Microsoft.Visual.Basic.dll library. The Visual Basic services are created in various namespaces. To use a service, you must know its namespace. You can then add the desired namespace to the top of the file where you want to use it. Then, in the document that contains your code, use the Visual Basic class you want.
Interoperability
Introduction
One of the most important sought goals in .NET is to allow different languages to collaborate, such as sharing code. One way this can happen is to be able to use the functionality of one language into a program created with another language. For example, you can use the rich library of the Visual Basic language in a C# application. As no library is ever complete, you may still need functionality that is not easily found in the language you are using. Furthermore, you may be working with a team of programmers who are using different languages and/or have already created complex operations. You should be able to use that existing code.
The Microsoft Windows operating system was originally written in C, the parent language of C++ and C# (also of Java). To allow programmers to create applications, Microsoft released a library called Win32. This is a series of functions and classes, etc, that you previously had to use. As time has changed, you do not need to exclusively use Win32 anymore to create a Windows application. Nonetheless, Win32 is still everywhere and it is not completely avoidable because many or some of the actions you would want to perform in a Windows application are still available only in Win32. Fortunately, in most cases, it is not always difficult to use some of these functions in a C# applications, as long as you observe some rules. Here is an example:
using System; using System.Runtime.InteropServices; class Program { [DllImport("Kernel32.dll")] public static extern bool SetConsoleTitle(string strMessage); static void Main() { SetConsoleTitle("C# Programming"); } }
A Visual C++/CLI Library
You can use a library created in Microsoft Visual C++ in your C# code. To create a library, first display the New Project dialog box. After specifying Visual C++, in the middle list, click Class Library and give it a name. In the body of the file, you can create the classes and/or functions as you see fit. Here is an example:
// Business.h #pragma once using namespace System; public ref class Finance { public: double CalculateDiscount(double MarkedPrice, double DiscountRate) { return MarkedPrice * DiscountRate / 100; } };
Once the project is ready, you must build it (on the main menu, Build -> Build Business). As a result, the compiler would create a file with the .dll extension. Normally, as far as creating a library, that's it.
Using the Library
Creating a library in C++ is easy. To use it, there are a few rules you must follow. To start, you must make sure that your project can "physically" find the library. Probably the easiest way to take care of this is to copy the dll file and paste it in the folder that contains your project's executable. You can also do this directly in Microsoft Visual Studio by importing the library file as we saw earlier.
In your project, you should include the System.Runtime.InteropServices namespace. Before the section where the library will be accessed, enter the DllImport attribute that takes as argument the name of the library passed as a string. Here is an example:
using System;
using System.Runtime.InteropServices;
using Business;
class Exercise
{
[DllImport("Business.dll")]
public static extern double CalculateDiscount(double price,
double discount)
static void Main()
{
Finance fin = new Finance();
double markedPrice = 275.50;
double discountRate = 25.00; // %
double discountAmount = fin.CalculateDiscount(markedPrice,
discountDate);
double netPrice = markedPrice - discountAmount);
Console.Write("Marked Price: ");
Console.WriteLine("markedPrice);
Console.Write("Discount Rate: ");
Console.WriteLine("discountRate / 100);
Console.Write("Discount Amount: ");
Console.WriteLine("discountAmount);
Console.Write("Net Price: ");
Console.WriteLine("netPrice);
}
}
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2001-2021, C# Key | Next |
|