CS计算机代考程序代写 compiler IOS android c++ c# C# Classes

C# Classes

C# Programming

Quincy College

Fall 2021

Robert I. Pitts

 Background on C# and .NET

 Structure of C# program

 Useful library classes

 Objects and their methods

 User-defined classes

 Properties

 UML for C# classes

Fall 2021C# Programming 2

 C#: Microsoft’s object-oriented language for
application development

 Code is compiled to intermediate language (CIL,
formerly called MSIL)

 .NET: Microsoft’s framework
for applications
◦ Framework Class Library

◦ Common Language Runtime (CLR)

 Uses Just-In-Time (JIT) compilation to translate CIL to
machine code

Fall 2021C# Programming 3

https://docs.microsoft.com/en-us/previous-versions/ms229335(v=vs.100)

 To run on platform need .NET Framework
◦ Windows 7/8/10, Xbox family, (discontinued) Phone

◦ Open Source Frameworks: Mono, .NET Core

◦ Tools: Xamarin (create native Win, Android, iOS apps)

 Program to .NET via multiple languages:
C#, VB, C++
◦ Compiler to produce CIL for language

◦ Library binding for language

 Free IDE: Visual Studio 2019 Community with latest
.NET Framework (microsoft.com)

Fall 2021C# Programming 4

https://www.mono-project.com/
https://www.microsoft.com/net/download
https://visualstudio.microsoft.com/downloads/

 Example Project: Alarm Clock
◦ Initially: Console Application

◦ Mid-semester: Windows Forms Application

◦ Time permitting: possibly Windows Presentation
Foundation (XML-based) or web-based

Fall 2021C# Programming 5

using System;

namespace AlarmClock

{

class Program

{

static void Main(string[] args)

{

}

}

}

 Conventions: Which use camel, Pascal case?

Fall 2021C# Programming 6

Project in namespace

Class
for main
program

Method starts program

Use library code

 Console.Write/WriteLine

◦ Display values to screen

 Lookup under Microsoft Documentation: search
for “Console class C#”
◦ Version takes format string, placeholders like{0}

for items listed as arguments
Console.Write(“{1}, {0}”, fName, lName);

 Console.ReadLine

◦ Reads keyboard input until newline (returned
string does not include newline)

Fall 2021C# Programming 7

https://docs.microsoft.com/en-us/

 Composite Formatting Feature: {0:format}

 Examples:

Console.Write(“{0:C}”, 4.5);

string s =

string.Format(“{0:format}”, value);

string t = obj.ToString(“format”);

 From string to number, use Convert class

◦ Example: int mth = Convert.ToInt32(“7”);

Fall 2021C# Programming 8

Format as currency
(internationalized code)

https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting

 Logical organization for related classes
◦ Help avoid name conflicts

 Use class in namespace
◦ Fully-qualified name: System.Console

◦ Using directive: using System;

 Then use unqualified class name

 Namespace alias
using NewName = OldName;

Ex: using Console = System.Console;

Fall 2021C# Programming 9

 Simple types are value types
int n = 4;

 Classes are reference types
string s = “abc”;

Fall 2021C# Programming 10

n

4
Memory for variable holds
actual value (4)

s string object

abc

(hold address)

 Class string (data type): store sequence of
characters, extract substring, …

 Object is actual instance of type
string s = “abc”;

 Question: t.Substring(1) gives?
◦ What happens if do: t = s?

Fall 2021C# Programming 11

s

abc

t

xyz

, t = “xyz”;

 Start with object
string s = “abc”;

DateTime d = new DateTime(2014, 1, 1);

 Call method using member access operator (.)
s.Substring(1, 2)

d.IsDaylightSavingTime()

 Question: Why can call with class name, as in:
Console.ReadLine()?
◦ Method marked static do not require object

Fall 2021C# Programming 12

 Encapsulation: hide internal details of how data
stored, how operations work

 User-defined class
◦ Field: data stored (“attribute” in OOP)

Also called instance variables (if not static)

◦ Method: code that executes (“functions”)

 Pass arguments / return value

◦ Constructor: special method to initialize object

 Same name as class, no return type

 Invoked when create object with new

Fall 2021C# Programming 13

 Default for members is “private” (but should
specify explicitly)
◦ Cannot access outside of class

 “public” allows access to field/method outside
class

 Make data “private”, some methods “public”

 Example:
private int hour;

public Alarm(string time)

 Question: How access hour outside class?

Fall 2021C# Programming 14

 Property: succinct get/set accessor
private type varName; // instance var

public type PropName // property

{

get { return varName; }

set { varName = value; }

}

◦ Convention: instance variable (camel case) 
property (Pascal case)

 Access like instance variable: obj.PropName
 Translate between formats, detect invalid values
 Also use property inside methods of class

Fall 2021C# Programming 15

 Auto-Implemented: Property without listing
instance variable
public type Name { get; set; }

◦ Compiler creates variable
◦ Can only access via property

 Code Snippet: Type “prop” Tab Tab (or right-click,
Insert Snippet…”)

 One accessor may be more restrictive than
property
◦ Ex: public property, but private “set”

Fall 2021C# Programming 16

 Visibility: public (+), private (-)

 Syntax of :type (C# types allowed)

 Mark special features with stereotype in
guillemets (e.g., «property»)

Fall 2021C# Programming 17

Alarm

-hour : int

+«property» Hour : int

+«constructor» Alarm(time : string)

 .NET not only supports multiple platforms, but
may be targeted by multiple languages

 Library and user-defined code is organized into
namespaces

 All reference (class) types store address of object,
value (simple) types are stored directly in variable

 Properties provide concise way to control access
to instance variables

Fall 2021C# Programming 18

 null value for reference

 Default constructor

 decimal data type

Fall 2021C# Programming 19