Introduction to C# classes

A class is a group of related methods and variables. A class describes these things, and in most cases, you create an instance of this class, now referred to as an object. On this object, you use the defined methods and variables. Of course, you can create as many instances of your class as you want to.

When you define a class, you define a blueprint for a data type. This does not actually define any data, but it does define what the class name means. That is, what an object of the class consists of and what operations can be performed on that object. Objects are instances of a class. The methods and variables that constitute a class are called members of the class.

A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces.

Example of a Class is given below:
class Bike
{
private string color;
public Bike (string color)
{
this.color = color;
}
public string Show()
{
return "This bike is " + Color;
}
public string Color
{
get { return color; }
set { color = value; }
}
}
Above code we have defined a new class, called Bike.. It defines a single variable, called color, which of course is used to tell the color of bike. We declared it as private, which is good practice - accessing variables from the outside should be done using a property. The Color property is defined in the end of the class, giving access to the color variable. Besides that, our Bike class defines a constructor. It takes a parameter which allows us to initialize Bike objects with a color. Since there is only one constructor, Bike objects can only be instantiated with a color. The Show() method allows us to get a nice message with the single piece of information that we record about bike color. It simply returns a string with the information we provide.
Note:
1. Access specifiers specify the access rules for the members as well as the class itself. If not mentioned, then the default access specifier for a class type is internal. Default access for the members is private.
2. Data type specifies the type of variable, and return type specifies the data type of the data the method returns, if any.
3. To access the class members, you use the dot (.) operator.
4. The dot operator links the name of an object with the name of a member.

Read More

आइये जानते हैं Android 11 के बेस्ट 11 फीचर्स

आपके स्मार्टफोंस को और भी स्मार्ट बनाते हुए तथा मोबाइल तकनीक को और भी एडवांस करते हुए टेक दिग्गज़ Google ने अपना नया ऑपरेटिंग सिस्टम Android 11 दुनिया ...

StringBuilder in C#

A String is immutable, meaning String cannot be changed once created. For example, new string "Hello World!!" will occupy a memory space on the heap. ...

Properties in C#

In C#, properties are nothing but natural extension of data fields. They are usually known as smart fields in C# community. We know that data encaps ...

C# Hashtable

The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the ...

C# Queue

C# includes a Queue collection class in the System.Collection namespace. Queue stores the elements in FIFO style (First In First Out), exactly opposit ...

C# Stack

C# includes a special type of collection which stores elements in LIFO style(Last In First Out). C# includes a generic and non-generic Stack. Here, y ...

C# SortedList

The SortedList collection stores key-value pairs in the ascending order of key by default. SortedList class implements IDictionary & ICollection inter ...

C# ArrayList

ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automa ...

Collection in C#

C# includes specialized classes that hold many values or objects in a specific series, that are called collection. Collection classes are specialized ...

C# File Stream I/O

A file is a collection of data stored in a disk with a specific name and a directory path. C# includes static File class to perform I/ ...

Exception Handling in C#

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while ...

Recent Posts






















Like us on Facebook