Giter Site home page Giter Site logo

fortune_teller's Introduction

Anatomy of a C# Program

This is a high-level overview of the basic structure of a C# program. Much of what is covered here will be confusing, but don't worry we will cover it in more detail as we move throughout the course.

File Structure

Every C# program is made up of at least two files: a project file and a C# file.

For example, a "Fortune Telling" app might have this structure.

FortuneTeller/
├── FortuneTeller.csproj
└── Program.cs

The *.csproj file is the project file. It contains configuration information about the application. There is only one project file per project.

A *.cs file is a C# source code file. This is where your C# code goes. There can be many *.cs files. A large project could easily have hundreds or more.

In addition to these files you will also find two folders called bin and obj. These folders contain the output of compiling your C# code.

Program.cs

Program.cs is usually the starting point of you C# program. It contains a class called Program with method called Main. The Main method is always where the code starts executing.

using System;

namespace FortuneTeller
{
    class Program
    {
        static void Main(string[] args)
        {
            string fortune = "Everything will be ok.";
            Console.WriteLine(fortune);
        }
    }
}

Let's walk through this code

using System;

This is the way we "import" parts of the .net class library. The System "namespace" contains the Console object that we use later in the program.

namespace FortuneTeller

A namespace is a collection of related classes.

class Program

Almost all C# code must be contained inside a class. It is convention to use the same name for the class and the *.cs file.

static void Main(string[] args)

Main is a "method" in the Program class. A method is a function that exists inside a class. Main is a "static" method. This means we do not need to create a Program object in order to use it.

string fortune = "Everything will be ok.";
Console.WriteLine(fortune);

These lines make up the "body" of the Main method. The "body" of a method contains the executable C# code. Here we create a string variable and print it to the Console.

Notes: Made app using dotnet new console -o FortuneTeller2

Run app in console using dotnet run

fortune_teller's People

Contributors

laceywalkerr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.