程序代写代做代考 F# C assembly android Java IOS go c++ compiler c# C# Overview

C# Overview
Cpt S 321 Washington State University

.NET 101
• .NET: an open-source developer platform to build different types of applications. It includes different languages, libraries, and tools.
• Languages: C#, VisualBasic, F#
• Different implementations of .NET (also called different platforms):
• .NET Framework: a platform for websites, services, desktop apps, and more on Windows
• .NET Core: a cross-platform implementation, i.e., it runs on everything (Windows, Linux, and macOS)
• Xamarin/Mono: a .NET implementation for mobile devices (e.g., iOS and Android)
• .NET Standard: a formal specification of the APIs that are common across .NET implementations.
This allows the same code and libraries to run on different implementations.
• Originally came from Microsoft, but other developers and companies have started to contribute

C# Basics
• Managed language – dynamically allocated content is automatically freed after it is determined to no longer be in use.
• Disposal: explicitly invoked
• Garbage collection: automatic
• Pointers are not eliminated – they are simply unnecessary most of the time
• Syntax is similar (but definitely not identical) to C++ (VERY similar to Java)
• Has lots of preexisting code associated with it, through the different frameworks such as the .NET Framework

The C# 8.0 in a Nutshell Book Topics

C# Basics
● Multi-paradigm programming language with:
○ strongtyping
○ imperative ○ declarative ○ functional ○ generic
○ object-oriented
○ event-driven
○ component-oriented
–predominatelystaticallytypedlanguage; enforces static and dynamic type checking
–canusestatementstochangestate –canuselogicflowparadigms –canfitasmathematicalfunctions –allowstemplatingandvirtualbaseclass –actually,it’sallinobjectshere:
Unified Type System –theflowisdeterminedbyevents –designedtohelpdefinetheuseof objects as services and coherent behaviors

Similarities with C++
• Has
• classes, structures,
• encapsulation, information hiding (sometimes called abstraction), inheritance, and polymorphism
• Has access modifiers (public, private, protected): default versus explicitly specifying them
• Dealing with many basic types (int, short, char, bool) is the same
• For-loops and while-loops are just about the same

Differences from C++
• Arrays are managed objects – not just a pointer to an address
• EVERYTHING is a class or a structure. Integers (int type) are structures,
strings are classes.
• No pointers (without the “unsafe” language subset, which does support them)
• char* is no longer what we use for a string. We use the string class.
• Classes are reference types, structures are value types (this is an important
one; more on this in a few slides)
• Structures do NOT support inheritance, only classes do
• No globals, everything is declared inside a class

Differences from C++
• C# has foreach loops -> since added in C++11
• Variables cannot be used without first being initialized in C# int x;
x+= 5; // Compiler error
• Dynamically allocated objects are automatically freed
• Syntax for accessing members of objects in C# is simplified. It’s always just the dot (.) not the arrow (->)
• Works for accessing methods, properties and fields of objects, regardless of whether they are classes or structures.
• Works for access things declared in other namespaces
• One small exception for unsafe code, but we’re not going to be dealing much with that

Variable Declarations in C#
• Local variables in functions aren’t all that different than C++
• Member variables are similar as well, but each member gets it’s own access definition:
C++
C#
class MyClass {
private:
int m_number;
string m_name; };
class MyClass {
private int number;
private string name; }

C# Access Modifiers
• You don’t HAVE to (but you better do in this class!) specify an access modifier before each variable.
• Internal is the default for classes and structures if no access modifier is specified. • Private is default for members of classes and structures.
• Supported access modifiers in C# (for both structs and classes): • public
• internal: can be accessed by any code in the same assembly, but not from another assembly. • private
• Additional supported access modifiers for classes only:
• private protected: can be accessed by code in the same class or in a derived class within the base
class assembly.
• protected internal: can be accessed by any code in the same assembly, or by any derived class in another assembly.
• protected: can be accessed by code in the same class or in a derived class.
• Recall that it was previously mentioned that structures have no inheritance. So protected wouldn’t make sense for them.

Reference Types vs. Value Types
• Recall from C++: there’s a significant difference between dealing with pointers and values. Passing an object by value to a function is different from passing it by reference or pointer.
• Predefined value types in C#
• Numeric: integer (sbyte, short, int, long and byte, ushort, uint, ulong) and real (float,
double, decimal) numbers
• Logical (bool)
• Character (char)
• Predefined reference types in C# • String
• Object
• User defined types: Classes are reference types, structures are value types • Further reading: Choosing between Class and Struct

Reference Types vs. Value Types – examples
• Example of a reference type.
Reference
1. s 2. s2
Object
“Hello”
1. string s = “Hello”;
2. string s2 = s;
// The variable s is implicitly a // reference to a string object // in this case because of the // fact that string is a class.
// This copies a reference.
//Now we have two variables // referencing the same string.
• Example of a value type. 1. intx=5,y=6;
x y
1. 2. 3.
5 5 10 655
2. y=x; 3. x+=5;
//Copiesthevalueofxtoy //Doesnotaffectyatall

What Can We Declare in a C# Class or Structure?
• Methods, properties, and fields (also called attributes) public class BasicMessageClass
{ string message = “(default message)”; // FIELD; what is the access modifier here? public void ShowMessageConsole() { Console.WriteLine(this.message); } // METHOD
// public void ShowMessageConsole() => Console.WriteLine(this.message); // Expression-bodied member
// equivalent to the previous line
public string Message // PROPERTY : Acts a lot like a field to code outside of the class, but it is actually an accessor method;
{
} }
get { return message; } set { message = value; }
it promotes encapsulation
// equivalent to: get => message;
// equivalent to: set => message = value;
BasicMessageClass someBasicMessage = new BasicMessageClass(); someBasicMessage.Message = “new message!”
• Somewhere outside this class:

Properties can be more than simple accessors/modifiers
public class AngleClass
{
private double angleRadians; // angle in radians
public double AngleDegrees // PROPERTY – angle in degrees
{
get { return angleRadians * 180.0 / Math.PI; }
set { angleRadians = value / 180.0 * Math.PI; }
}
}
• Allowsyoutoperformlogicwhensettingthefield.Forexample,sayyouhaveclassthat stores an angle value, in radians. You could make a property that allows you get or set that value using degrees.
• Agetter(read-onlyproperty)orsetter(write-onlyproperty)orboth(read-write)

Inheritance
A, B, C, D: classes Arrow: inheritance
Not allowed in C#:
A
C
D
B
• C# supports inheritance, but not multiple inheritance
• Have interfaces to deal with the lack of multiple inheritance. Classes and structures can implement multiple interfaces.
• An interface defines a contract
• An interface is similar to an abstract base class but interfaces may not contain
instance state
• Typically behavior is not to be implemented in an interface (only specified in terms of signature). However in C# 8.0, default implementation for members is allowed. Interfaces may also define static members.
• One inheritance exception: ALL classes and structures automatically inherit from “object”, which is a base class for all objects in the language. Has methods “Equals”, “GetHashCode”, “ToString” and a few others.

Class and Structure Declarations and Implementations
• Declared and implemented in the same place. No more .h file to define and then have a separate .cpp to implement.
• C# code files (.cs files) can define multiple classes and structures and implement them in one code file (it does NOT mean you should!).
• Other files that want to use classes/structs declared in another .cs file don’t have to include it (no more #include statements). If it’s in the project, then it can be used by all other pieces of code in the project.

.NET Framework
• Prewritten code at your disposal
• Organized into namespaces. Recall how cin, cout, vector and other things
were in the std namespace in C++. Namespaces in C# function similarly.
• Predefined types in C# map to types in the System namespace. • string -> System.String
• int -> System.Int32
• short -> System.Int16
• and many more…
• Most of the basic types are in the system namespace. Almost all .cs code files have “using System;” as one of the first statements in them. Note that in C++ it was “using namespace …” now it’s just “using …”.

.NET Framework
• Lots of code in .NET framework. • File I/O
• Network I/O
• Encryption
• Compression
• Hash tables
• Lists, stacks and queues (System.Collections.Generic namespace) • Strings and regular expressions
• BigInteger, Complex (System.Numerics namespace) • Image processing
• UI components (WinForms and WPF)
• and much more…

Let’s touch base!
• Any questions regarding the syllabus?
• Please go to BB and do the survey: Fill it in, keep it open, and submit before you leave class!
• Osman is joining our TA team! Make sure you add him to your repository and you cc him in your emails.
• HW1 posted, have fun! Note: Think about it but do NOT start coding yet: we will talk about testing first BEFORE you write any code!
• C# Tutorials