C# is a strongly typed object-oriented programming language. C# is open source, simple, modern, flexible, and versatile. In this article, let’s learn what C# is, what C# can do, and how C# is different than C++ and other programming languages.
A programming language on computer science is a language that is used to write software programs.
C# is a programming language developed and launched by Microsoft in 2001. C# is a simple, modern, and object-oriented language that provides modern day developers flexibility and features to build software that will not only work today but will be applicable for years in the future.
Key characteristics of C# language include:
- Modern and easy
- Fast and open source
- Cross platform
- Safe
- Versatile
- Evolving
C# is modern and easy
C# is a simple, modern, and an object-oriented programming language. The purpose of C# was to develop a programming language that is not only easy to learn but also supports modern day functionality for all kind of software development.
If you look at the history of programming languages and their features, each programming language was designed for a specific purpose to solve a specific need at that time.
C# language however was designed to keep business and enterprises needs in mind. C# language was designed for businesses to build all kinds of software by using one single programming language.
C# provides functionality to support modern day software development. C# supports Web, Mobile, and app development needs. Some of the modern-day programming language features C# supports are generics, var types, auto initialization of types and collections, lambda expressions, dynamic programming, asynchronous programming, tuples, pattern matching, advanced debugging and exception handling, and more.
C# language syntaxes are influenced from C++, Java, Pascal and few other languages that are easy to adopt. C# also avoids complexity and unstructured language features.
C# is fast and open source
C# is open source under the .NET Foundation, which is governed and run independently of Microsoft. C# language specifications, compilers, and related tools are open source projects on Github. While C# language feature design is lead by Microsoft, the open source community is very active in the language development and improvements.
C# is fast compare to several other high-level programming languages. C# 8 has many performance improvements.
C# is cross platform
C# is cross platform programming language. You can build .NET applications that can be deployed on Windows, Linux, and Mac platforms. C# apps can also be deployed in cloud and containers.
C# is safe and efficient
C# is a type safe language. C# does not allow type conversions that may lead to data loss or other problems. C# allows developers to write safe code. C# also focuses on writing efficient code.
Here is a list of some of the key concepts in C# that helps write safe and efficient code.
- Unsafe type casting is not allowed.
- Nullable and non-nullable types are supported in C#.
- Declare a readonly struct to express that a type is immutable and enables the compiler to save copies when using in parameters.
- Use a ref readonly return when the return value is a struct larger than IntPtr.Size and the storage lifetime is greater than the method returning the value.
- When the size of a readonly struct is bigger than IntPtr.Size, you should pass it as an in parameter for performance reasons.
- Never pass a struct as an in parameter unless it's declared with the readonly modifier because it may negatively affect performance and could lead to an obscure behavior.
- Use a ref struct, or a readonly ref struct such as Span<T> or ReadOnlySpan<T> to work with memory as a sequence of bytes.
References
C# is versatile
C# is a Swiss army knife. While most programming languages were designed for a specific purpose, C# was designed to
do C#. We can use C# to build today’s modern software applications. C# can be used to develop all kind of applications including Windows client apps, components and libraries, services and APIs, Web applications, Mobile apps, cloud applications, and video games.
Here is a list of types of applications C# can build,
- Windows client applications
- Windows libraries and components
- Windows services
- Web applications
- Web services and Web API
- Native iOS and Android mobile apps
- Backend services
- Azure cloud applications and services
- Backend database using ML/Data tools
- Interoperability software such as Office, SharePoint, SQL Server and so on.
- Artificial Intelligence and Machine learning
- Blockchains and distributed ledger technology including cryptocurrency
- Internet of Things (IoT) devices
- Gaming consoles and gaming systems
- Video games
C# is evolving
C# 8.0 is the latest version of C#. If you look at C# language history, C# is evolving faster than any other languages. Thanks to Microsoft and a strong community support. C# was initially designed to write Windows client applications but today, C# can do pretty much anything from console apps, cloud app, and modern machine learning software.
The following table summarizes the C# versions with year and features.
Version
|
Year
|
Features
|
1.0
|
1999-2002
|
Modern, Object Oriented, Simple, Flexible, Typesafe, Managed, Garbage Collection, Cross-platform
|
2.0
|
2005
|
Generics, Anonymous Method, Partial Class, Nullable Type
|
3.0
|
2008
|
LINQ, Lamda Expression, Extension Method, Anonymous Type, Var
|
4.0
|
2010
|
Named and Optional Parameters, Dynamic Binding
|
5.0
|
2012
|
Async Programming
|
6.0
|
2015
|
Compiler-as-a-service (Roslyn), Exception filters, Await in catch/finally blocks, Auto property initializers, Dictionary initializer, Default values for getter-only properties, Expression-bodied members. Null propagator, String interpolation, nameof operator
|
7.0
|
2017
|
Tuples, Out variables, Pattern matching, Deconstruction, Local functions, Digit separators, Binary literals, Ref returns and locals, Generalized async return types, Expression bodied constructors and finalizers, Expression bodied getters and setters, Throw can also be used as expression
|
7.1
|
2017
|
Async main, Default literal expressions, Inferred tuple element names
|
7.2
|
2017
|
Reference semantics with value types, Non-trailing named arguments, Leading underscores in numeric literals, private protected access modifier
|
7.3
|
2018
|
Accessing fixed fields without pinning, Reassigning ref local variables, Using initializers on stackalloc arrays, Using fixed statements with any type that supports a pattern, Using additional generic constraints
|
8.0
|
2019
|
Nullable reference types, Async streams, ranges and indices, default implementation of interface members, recursive patterns, switch expressions, target-type new expressions
|
C# Strings
In any programming language, to represent a value, we need a data type. The Char data type represents a character in .NET. In .NET, the text is stored as a sequential read-only collection of Char data types. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0').
The System.String data type represents a string in .NET. A string class in C# is an object of type System.String. The String class in C# represents a string.
The following code creates three strings with a name, number, and double values.
-
- System.String authorName = "Mahesh Chand";
-
-
- System.String age = "33";
-
-
- System.String numberString = "33.23";
Here is the complete example that shows how to use stings in C# and .NET.
- using System;
- namespace CSharpStrings
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
- System.String authorName = "Mahesh Chand";
-
-
- System.String age = "33";
-
-
- System.String numberString = "33.23";
-
-
- Console.WriteLine("Name: {0}", authorName);
- Console.WriteLine("Age: {0}", age);
- Console.WriteLine("Number: {0}", numberString);
- Console.ReadKey();
- }
- }
- }
String class defined in the .NET base class library represents text as a series of Unicode characters. The String class provides methods and properties to work with strings.
The String class has methods to clone a string, compare strings, concatenate strings, and copy strings. This class also provides methods to find a substring in a string, find the index of a character or substrin.g, replace characters, split a string, trim a string, and add padding to a string. The string class also provides methods to convert a string's characters to uppercase or lowercase.
C# Arrays
An Array in C# is a collection of objects or types. C# Array elements can be of any type, including an array type. An array can be Single-Dimensional, Multidimensional or Jagged. A C# Array can be declared as fixed length or dynamic. An Array in C# can be a single dimension, multi dimension, or a jagged array. Learn how to work with arrays in C#.
In C#, an array index starts at zero. That means the first item of an array starts at the 0thposition. The position of the last item on an array will total number of items - 1. So if an array has 10 items, the last 10th item is at 9th position.
In C#, arrays can be declared as fixed length or dynamic. A fixed length array can store a predefined number of items. A dynamic array does not have a predefined size. The size of a dynamic array increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined.
Let's take a look at simple declarations of arrays in C#. The following code snippet defines the simplest dynamic array of integer types that do not have a fixed size.
int[] intArray;
As you can see from the above code snippet, the declaration of an array starts with a type of array followed by a square bracket ([]) and name of the array.
The following code snippet declares an array that can store 5 items only starting from index 0 to 4.
- int[] intArray;
- intArray = new int[5];
The following code snippet declares an array that can store 100 items starting from index 0 to 99.
- int[] intArray;
- intArray = new int[100];
C# Collections
C# collection types are designed to store, manage and manipulate similar data more efficiently. Data manipulation includes adding, removing, finding, and inserting data in the collection. Collection types implement the following common functionality:
- Adding and inserting items to a collection
- Removing items from a collection
- Finding, sorting, searching items
- Replacing items
- Copy and clone collections and items
- Capacity and Count properties to find the capacity of the collection and number of items in the collection
.NET supports two types of collections, generic collections and non-generic collections. Prior to .NET 2.0, it was just collections and when generics were added to .NET, generics collections were added as well.
Learn More C#
C# Corner has thousands of tutorials, code samples, and articles on C# programming. Here is a list of some of the most popular C# programming topics,
C# Books
C# Corner provides several C# language programming books. Here are a few:
Summary
This tutorial is an introduction to C# language for beginners. In this tutorial, we learned how to write our first C# program, basics of data types, classes, objects, and class members.