Learn how to create a simple array using primitives in C#.
- [Instructor] We've done a lot of work in our Characterlibrary, but libraries don't have any way to execute, they are not executable programs by themselves. They're just a library of code that's meant to be consumed from another program. In this course we're going to actually make two programs that will consume this and the first is going to be a console app because as I mentioned earlier, this is one of simplest forms of application that you can make. Since it is so simple it provides a gentle introduction into how to consume this library.
So why don't we get started. In your Solution Explorer, right click the Solution the top one, CharacterGenerator. Right-click it select Add New Project. In this case we're looking for the .NET core console application. And you'll find that under .NET Core and then App. Go ahead a click it and then click Next. As before we need to give our project a name, but this time you can see that the Solution Name is already filled in.
So all we have to do is just give it a Project Name and I'm going to call this project CharacterCommand. I'll go ahead and click Create and we'll see the familiar Hello World program that was generated in an earlier chapter. Visual Studio always gives you Hello World as the starting point for a command line application. The next step in consuming our Characterlibrary within our CharacterCommand Program is to add it as a reference.
To do this I'll come over to the CharacterCommand project, click on Dependencies, right-click and select Edit References. You're going to get this tabbed dialogue box that lets you select what it is that you want to add. In this case there really is only one thing because there's only one other thing in the project and that's CharacterLibrary, but you could actually find other things to add, so if you were to go into Package you could search for any Packages that you have installed.
Projects is where we're actually pulling our library from, but the All tab shows everything. .NET Assembly will allow you to import a DLL or a complied library from some other location on your system. In this case we want the dependency to come from, again, our CharacterLibrary Project here. So I'm just going to click the checkbox and click OK. With the dependency added I need to add a using statement to the top.
This is where that namespace we talked about earlier comes in handy. You can see on line four that this project's namsepace is CharacterCommand, earlier our project in Characterlibrary had a namespace of Characterlibrary. So what I need to do is type in using Characterlibrary. When I add my using statement, I'm effectively adding all of the public classes and all the public methods within those classes as well as their properties to this project so I can access them as though they were part of this project.
This advantage in doing this is of course that you can have one library shared amongst many programs which is exactly what our end goal is. We're going to work with a commandline project and then later a desktop project but there's nothing to stop you from using the same approach to developing something that would run on the web as well as on a mobile application, a desktop application, really any kind of program that you can make in Visual Studio. In the next video we'll actually put our Characterlibrary to use.
I'll show you how to actually access the class that we created in there earlier, the Character class as well as all of its methods and properties.
- Exploring C# on a Mac
- Creating a reusable code library
- Classes and properties
- Loops, arrays, and lists
- Creating a console app
- Creating a command-line app
- Creating a Mac desktop app
- Creating a UI with view controllers and actions