Learn C#_ Learn C#_ Classes and Objects Cheatsheet _ Codecademy
Learn C#_ Learn C#_ Classes and Objects Cheatsheet _ Codecademy
Cheatsheets / Learn C#
C# Classes
In C#, classes are used to create custom types. The using System;
class defines the kinds of information and methods
included in a custom type.
namespace BasicClasses
{
class Forest {
public string name;
public int trees;
}
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 1/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Constructor
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 2/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Parameterless Constructor
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 3/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Access Modifiers
In C#, members of a class can be marked with access public class Speech
modifiers, including public and private . A public
{
member can be accessed by other classes. A private
member can only be accessed by code in the same private string greeting = "Greetings";
class.
By default, fields, properties, and methods are private,
private string FormalGreeting()
and classes are public.
{
return $"{greeting} and salutations";
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 4/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Field
In C#, a field stores a piece of data within an object. It public class Person
acts like a variable and may have a different value for
{
each instance of a type.
A field can have a number of modifiers, including: private string firstName;
public , private , static , and readonly . If no access private string lastName;
modifier is provided, a field is private by default.
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 5/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# this Keyword
In C#, the this keyword refers to the current instance // We can use the this keyword to refer
of a class.
to the current class’s members hidden by
similar names:
public NationalPark(int area, string
state)
{
this.area = area;
this.state = state;
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 6/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Members
In C#, a class contains members, which define the kind class Forest
of data stored in a class and the behaviors a class can
{
perform.
public string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
C# Dot Notation
In C#, a member of a class can be accessed with dot string greeting = "hello";
notation.
// Prints 5
Console.WriteLine(greeting.Length);
// Returns 8
Math.Min(8, 920);
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 7/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Class Instance
In C#, an object is an instance of a class. An object can Burger cheeseburger = new Burger();
be created from a class using the new keyword.
// If a class is a recipe, then an object
is a single meal made from that recipe.
C# Property
// Prints "Louie"
Console.WriteLine(f.FirstName);
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 8/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Auto-Implemented Property
C# Static Constructor
In C#, a static constructor is run once per type, not per class Forest
instance. It must be parameterless. It is invoked before
{
the type is instantiated or a static member is accessed.
static Forest()
{
Console.WriteLine("Type
Initialized");
}
}
// In this class, either of the following
two lines would trigger the static
constructor (but it would not be
triggered twice if these two lines
followed each other in succession):
Forest f = new Forest();
Forest.Define();
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 9/10
10-02-2024 16:04 Learn C#: Learn C#: Classes and Objects Cheatsheet | Codecademy
C# Static Class
In C#, a static class cannot be instantiated. Its //Two examples of static classes calling
members are accessed by the class name.
static methods:
This is useful when you want a class that provides a set
of tools, but doesn’t need to maintain any internal data.
Math is a commonly-used static class. Math.Min(23, 97);
Console.WriteLine("Let's Go!");
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-classes/cheatsheet 10/10