This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
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 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.
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.
For more information, seeMembers.
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
.
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.
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.
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.
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.
A class, struct, or record can be nested within another class, struct, or record. For more information, seeNested 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.
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.
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.
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.
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).
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:
with
expression lets you create a new record instance that is a copy of an existing instance but with specified property values changed.ToString
method prints the record type name and the names and values of public properties.For more information, seeRecords.
For more information, see theC# Language Specification. The language specification is the definitive source for C# syntax and usage.
Was this page helpful?
Was this page helpful?