Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Overview of object oriented techniques in C#

  • 2025-04-19
Feedback

In this article

In C#, the definition of a type—a class, struct, or record—is like a blueprint that specifies what the type can do. An object is basically a block of memory allocated and configured according to the blueprint. This article provides an overview of these blueprints and their features. Thenext article in this series introduces objects.

Encapsulation

Encapsulation is sometimes referred to as the first pillar or principle of object-oriented programming. A class or struct can specify how accessible each of its members is to code outside of the class or struct. Members not intended for consumers outside of the class or assembly are hidden to limit the potential for coding errors or malicious exploits. For more information, see theObject-oriented programming tutorial.

Members

Themembers of a type include all methods, fields, constants, properties, and events. In C#, there are no global variables or methods as there are in some other languages. Even a program's entry point, theMain method, must be declared within a class or struct (implicitly when you usetop-level statements).

The following list includes all the various kinds of members that can be declared in a class, struct, or record.

  • Fields
  • Constants
  • Properties
  • Methods
  • Constructors
  • Events
  • Finalizers
  • Indexers
  • Operators
  • Nested Types

For more information, seeMembers.

Accessibility

Some methods and properties are meant to be called or accessed from code outside a class or struct, known asclient code. Other methods and properties might be only for use in the class or struct itself. It's important to limit the accessibility of your code so that only the intended client code can reach it. You specify how accessible your types and their members are to client code by using the following access modifiers:

The default accessibility isprivate.

Inheritance

Classes (but not structs) support the concept of inheritance. A class that derives from another class, called thebase class, automatically contains all the public, protected, and internal members of the base class except its constructors and finalizers.

Classes can be declared asabstract, which means that one or more of their methods have no implementation. Although abstract classes can't be instantiated directly, they can serve as base classes for other classes that provide the missing implementation. Classes can also be declared assealed to prevent other classes from inheriting from them.

For more information, seeInheritance andPolymorphism.

Interfaces

Classes, structs, and records can implement multiple interfaces. To implement from an interface means that the type implements all the methods defined in the interface. For more information, seeInterfaces.

Generic Types

Classes, structs, and records can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example, theList<T> class in theSystem.Collections.Generic namespace is defined with one type parameter. Client code creates an instance of aList<string> orList<int> to specify the type that the list holds. For more information, seeGenerics.

Static Types

Classes (but not structs or records) can be declared asstatic. A static class can contain only static members and can't be instantiated with thenew keyword. One copy of the class is loaded into memory when the program loads, and its members are accessed through the class name. Classes, structs, and records can contain static members. For more information, seeStatic classes and static class members.

Nested Types

A class, struct, or record can be nested within another class, struct, or record. For more information, seeNested Types.

Partial Types

You can define part of a class, struct, or method in one code file and another part in a separate code file. For more information, seePartial Classes and Methods.

Object Initializers

You can instantiate and initialize class or struct objects, and collections of objects, by assigning values to its properties. For more information, seeHow to initialize objects by using an object initializer.

Anonymous Types

In situations where it isn't convenient or necessary to create a named class you use anonymous types. Named data members define anonymous types. For more information, seeAnonymous types.

Extension Members

You can "extend" a class without creating a derived class by creating a separate type. That type contains methods that can be called as if they belonged to the original type. For more information, seeExtension methods.

Implicitly Typed Local Variables

Within a class or struct method, you can use implicit typing to instruct the compiler to determine a variable's type at compile time. For more information, seevar (C# reference).

Records

You can add therecord modifier to a class or a struct. Records are types with built-in behavior for value-based equality. A record (eitherrecord class orrecord struct) provides the following features:

  • Concise syntax for creating a reference type with immutable properties.
  • Value equality.Two variables of a record type are equal if they have the same type, and if, for every field, the values in both records are equal. Classes use reference equality: two variables of a class type are equal if they refer to the same object.
  • Concise syntax for nondestructive mutation.Awith expression lets you create a new record instance that is a copy of an existing instance but with specified property values changed.
  • Built-in formatting for display.TheToString method prints the record type name and the names and values of public properties.
  • Support for inheritance hierarchies in record classes.Record classes support inheritance. Record structs don't support inheritance.

For more information, seeRecords.

C# Language Specification

For more information, see theC# Language Specification. The language specification is the definitive source for C# syntax and usage.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo