Build Web Apps with ASP.NET Flashcards
Build Web Apps with ASP.NET
What is .NET?
Formally, .NET is “an open source developer platform, created by Microsoft, for building many different types of applications. You can write .NET apps in C#, F#, Visual C++, or Visual Basic.”
Informally, .NET is the tool that lets you build and run C# programs (we’ll avoid F#, Visual C++, Visual Basic for now).
When you download .NET, you’re really downloading a bunch of programs that:
* Translate your C# code into instructions that a computer can understand
* Provide utilities for building software, like tools for printing text to the screen and finding the current time
* Define a set of data types that make it easier for you to store information in your programs, like text, numbers, and dates
There are a few versions of .NET. They do the same job but they are meant for different operating systems:
* .NET Framework is the original version of .NET that only runs on Windows computers.
* .NET Core is the new, cross-platform version of .NET that runs on Windows, MacOS, and Linux computers. Since it’s more flexible and Microsoft is actively enhancing this version, we’ll be using this one throughout the path.
Whenever we refer to “.NET” from now on, we really mean .NET Core. Conveniently, Microsoft itself plans to rename .NET Core to .NET towards the end of 2020.
Build Web Apps with ASP.NET
How do I get .NET?
So we know we want to work with C#, and we know we need .NET. How do we get the .NET platform on our computers?
The two main ways are:
1. Download Visual Studio, an integrated development environment (IDE) for .NET applications. It comes as an app, like the web browser you’re using to view this article. It comes with the .NET platform, a code editor, and additional tools to help you write code.
2. Download the .NET SDK (software development kit). It also comes with the .NET platform, but it has no code editing tools. Instead of an app on your computer, the SDK is accessed via a command-line interface (CLI) — to use an SDK, you’ll open up a terminal on your computer and type commands instead of clicking buttons. In this example, you can see a terminal in which the user ran commands like dotnet new and dotnet run to build a new application, then ran it (the app just prints out “Hello World!”).
Build Web Apps with ASP.NET
How do I get .NET?
So we know we want to work with C#, and we know we need .NET. How do we get the .NET platform on our computers?
The two main ways are:
1. Download Visual Studio, an integrated development environment (IDE) for .NET applications. It comes as an app, like the web browser you’re using to view this article. It comes with the .NET platform, a code editor, and additional tools to help you write code.
2. Download the .NET SDK (software development kit). It also comes with the .NET platform, but it has no code editing tools. Instead of an app on your computer, the SDK is accessed via a command-line interface (CLI) — to use an SDK, you’ll open up a terminal on your computer and type commands instead of clicking buttons. In this example, you can see a terminal in which the user ran commands like dotnet new and dotnet run to build a new application, then ran it (the app just prints out “Hello World!”).
Build Web Apps with ASP.NET
What about ASP.NET?
If we want to build programs specifically for the web, like websites, you’ll need to add more tools on top of .NET. One of the most popular is ASP.NET. To keep them separate, developers call .NET a platform and ASP.NET a framework. This is all you need to know about ASP.NET to get started with C#: we’ll cover the details in a separate article.
Build Web Apps with ASP.NET
Summary:
Hopefully, this helps you see the big picture before diving into the details of C#:
- C# is a programming language
- To build programs with C#, you need the .NET platform
- .NET comes in two major flavors, .NET Framework for Windows and .NET Core for Windows/MacOS/Linux
- To get .NET Core on your computer, download Visual Studio or the .NET SDK
- To build programs like web apps and web services, you need the ASP.NET framework on top of .NET
Build Web Apps with ASP.NET
Console.ReadLine()
The Console.ReadLine() method is used to get user input. The user input can be stored in a variable. This method can also be used to prompt the user to press enter on the keyboard.
Console.WriteLine("Enter your name: "); name = Console.ReadLine();
Build Web Apps with ASP.NET
Getting Input
In this example, the program writes a question to the console and waits for user input. Once the user types something and presses Enter (or Return), the input is printed back out to the user.
Console.WriteLine("How old are you?"); string input = Console.ReadLine(); Console.WriteLine($"You are {input} years old!");
The word input represents a variable. For now, know that the word input represents the text the user typed in the console. It’s labeled string because, in C#, a piece of text is called a string.
input is used in the following line so that the printed message will change based on what the user types. For example, if you ran the program and responded to the question with 101, then the value of input would be “101” and You are 101 years old! would be printed to the console.
Build Web Apps with ASP.NET
Comments
Comments are bits of text that are not executed. These lines can be used to leave notes and increase the readability of the program.
Single line comments are created with two forward slashes //.
Multi-line comments start with /* and end with */. They are useful for commenting out large blocks of code.
// This is a single line comment /* This is a multi-line comment and continues until the end of comment symbol is reached */
Build Web Apps with ASP.NET
Console.WriteLine()
The Console.WriteLine() method is used to print text to the console. It can also be used to print other data types and values stored in variables.
Console.WriteLine("Hello, world!"); // Prints: Hello, world!
Build Web Apps with ASP.NET
C# in the Wild
C# technologies are fast: A software company called Raygun built their application using Node.js, a framework for JavaScript. When their app started to slow down, they switched to .NET, a framework for C#. Their performance skyrocketed. As CEO John Daniel puts it, “using the same size server, we were able to go from 1,000 requests per second…with Node.js, to 20,000 requests per second with .NET Core.” In other words, they increased throughput by 2,000%.
DATA TYPES AND VARIABLES
Introduction to Data Types and Variables in C#
When we write programs, we’re telling the computer how to process pieces of information, like the numbers in a calculation, or printing text to the screen. So how does a computer distinguish between a number and a character? Why is it important to be able to tell the difference?
Languages like C# tell a computer about the type of data in its program using data types. Data types represent the different types of information that we can use in our programs and how they should be used.
Without data types, computers would try and perform processes that are impossible, like squaring a piece of text or capitalizing a number. That’s how we get bugs!
C# is strongly-typed, so it requires us to specify the data types that we’re using. It is also statically-typed, which means it will check that we used the correct types before the program even runs. Both language features are important because they help write scalable code with fewer bugs.
Using types will come up in several different places when learning C#. To start, we’ll examine how types impact variable declaration and the usage of different data types in a program.
In this lesson, we’ll look at:
Common C# data types
How to create variables that are statically typed
How to convert variables from one data type to another
Computers can do different things with different kinds of data. This computer will process data according to the function that you give it. If the function matches the data type, you will get an answer! If it doesn’t, you’ll see an error.
The functions do the following:
* capitalize: will turn lowercase characters into uppercase characters
* square: will square a number
* evaluate: will determine if an input is true or false
*
The data types include:
* int (4637): whole integer number
* string (kangaroo): a piece of text
* bool (true): represents the logical idea of true or false
DATA TYPES AND VARIABLES
C# Data Types
DATA TYPES AND VARIABLES
Variables and Types
A variable is a way to store data in the computer’s memory to be used later in the program. C# is a type-safe language, meaning that when variables are declared it is necessary to define their data type.
Declaring the types of variables allows the compiler to stop the program from being run when variables are used incorrectly, i.e, an int being used when a string is needed or vice versa.
string foo = "Hello"; string bar = "How are you?"; int x = 5; Console.WriteLine(foo); // Prints: Hello
DATA TYPES AND VARIABLES
Math.Sqrt()
Math.Sqrt() is a Math class method which is used to calculate the square root of the specified value.
double x = 81; Console.Write(Math.Sqrt(x)); // Prints: 9
DATA TYPES AND VARIABLES
Math.Sqrt()
Math.Sqrt() is a Math class method which is used to calculate the square root of the specified value.
double x = 81; Console.Write(Math.Sqrt(x)); // Prints: 9
DATA TYPES AND VARIABLES
Arithmetic Operators
Arithmetic operators are used to modify numerical values:
- addition operator
- subtraction operator
- multiplication operator
- / division operator
- % modulo operator (returns the remainder)
int result; result = 10 + 5; // 15 result = 10 - 5; // 5 result = 10 * 5; // 50 result = 10 / 5; // 2 result = 10 % 5; // 0
DATA TYPES AND VARIABLES
Unary Operator
Operators can be combined to create shorter statements and quickly modify existing variables. Two common examples:
++ operator increments a value.
– operator decrements a value
int a = 10; a++; Console.WriteLine(a); // Prints: 11
DATA TYPES AND VARIABLES
Math.Pow()
Math.Pow() is a Math class method that is used to raise a number to a specified power. It returns a number of double type.
double pow_ab = Math.Pow(6, 2); Console.WriteLine(pow_ab); // Prints: 36
DATA TYPES AND VARIABLES
.toUpper() in C#
In C#, .ToUpper() is a string method that converts every character in a string to uppercase. If a character does not have an uppercase equivalent, it remains unchanged. For example, special symbols remain unchanged.
string str2 = "This is C# Program xsdd_$#%"; // string converted to Upper case string upperstr2 = str2.ToUpper(); //upperstr2 contains "THIS IS C# PROGRAM XSDD_$#%"
DATA TYPES AND VARIABLES
IndexOf() in C#
In C#, the IndexOf() method is a string method used to find the index position of a specified character in a string. The method returns -1 if the character isn’t found.
string str = "Divyesh"; // Finding the index of character // which is present in string and // this will show the value 5 int index1 = str.IndexOf('s'); Console.WriteLine("The Index Value of character 's' is " + index1); //The Index Value of character 's' is 5
DATA TYPES AND VARIABLES
Bracket Notation
Strings contain characters. One way these char values can be accessed is with bracket notation. We can even store these chars in separate variables.
We access a specific character by using the square brackets on the string, putting the index position of the desired character between the brackets. For example, to get the first character, you can specify variable[0]. To get the last character, you can subtract one from the length of the string.
// Get values from this string. string value = "Dot Net Perls"; //variable first contains letter D char first = value[0]; //Second contains letter o char second = value[1]; //last contains letter s char last = value[value.Length - 1];
DATA TYPES AND VARIABLES
Escape Character Sequences in C#
In C#, an escape sequence refers to a combination of characters beginning with a back slash \ followed by letters or digits. It’s used to make sure that the program reads certain characters as part of a string. For example, it can be used to include quotation marks within a string that you would like to print to console. Escape sequences can do other things using specific characters. \n is used to create a new line.
DATA TYPES AND VARIABLES
Substring() in C#
In C#, Substring() is a string method used to retrieve part of a string while keeping the original data intact. The substring that you retrieve can be stored in a variable for use elsewhere in your program.
string myString = "Divyesh"; string test1 = myString.Substring(2);
DATA TYPES AND VARIABLES
String Concatenation in C#
Concatenation is the process of appending one string to the end of another string. The simplest method of adding two strings in C# is using the + operator.
// Declare strings string firstName = "Divyesh"; string lastName = "Goardnan"; // Concatenate two string variables string name = firstName + " " + lastName; Console.WriteLine(name); //Ths code will output Divyesh Goardnan
Welcome
-
@for (int i = 0; i < 3; i++)
{
- @i }
Welcome to my Razor Page
Title: @Model.Title
```@Model.FirstName
-
// Accessing the value of FavoriteFoods in PersonModel
@foreach (var food in Model.FavoriteFoods)
{
- @food }
My name is @Model.FirstName and I am @Model.Age years old
// Using a code block: @{ var greet = "Hey threre!"; var name = "John";@greet I'm @name!
} // Using parentheses:Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))
```Good morning, the time is: @time
} else if (time < 20) {Good day, the time is: @time
} else {Good evening, the time is: @time
} ```Today is Saturday
break; case "Sunday":Today is Sunday
break; default:Today is @day... Looking forward to the weekend
break; } ```The Avengers Are:
@for (int i = 0; i < @avengers.Count; i++) {@avengers[i]
} ```The Avengers Are:
@foreach (var avenger in avengers) {@avenger
} ```@i
i++; } ```@i
i++; } ```