Introduction to C# Versions
C# is an object-oriented language. It is very simple and powerful. This language is developed by Microsoft. C# first release occurred in the year 2002. Since then below released or versions has come. In this article, we will discuss the different versions.
Versions of C#
Here we will discuss the Versions of C#. Given below are the Versions of C#:
1. C# Version 1.0
This version is like java. Its lack in the async capabilities and some functionalities. The major features of this release are below
Classes: It is a blueprint that is used to create the objects.
- There can be only one public class per file.
- Comments can appear at the beginning or end of any line.
- If there is a public class in a file, the name of the file must match the name of the public class.
- If exists, the package statement must be the first line.
- import statements must go between the package statement(if there is one) and the class declaration.
- If there are no package or import statements, the class declaration must be the first line in the source code file.
- import and package statements apply to all classes within a source code file.
- File with no public classes can have a name that need not match any of the class names in the file.
Code:
// declaring public class
public class Test
{
// variable
public int a, b;
// member function
public void display()
{
WriteLine(“Class in C#”);
}
}
Structure: In Struct, we can store different data types under a single variable. We can use user-defined datatype in structs. We have to use the struct keyword to define this.
Code:
using System;
namespace ConsoleApplication {
// structure
public struct Emp
{
// different data types
public string Name;
public int Age;
public int Empno;
}
class Geeks {
// Main Method
static void Main(string[] args)
{
// Declare P1 of type Person
Person P1;
// P1's data
P1.Name = "Ram";
P1.Age = 21;
P1.Empno = 80;
// Displaying the values
Console.WriteLine("Data Stored in P1 is " +
P1.Name + ", age is " +
P1.Age + " and empno is " +
P1.empno);
}
}
}
Interfaces:
- The interface is used as a contract for the class.
- All interface methods are implicitly public and abstract.
- All interface variables are public static final.
- static methods not allowed.
- The interface can extend multiple interfaces.
- Class can implement multiple interfaces.
- Class implementing interface should define all the methods of the interface or it should be declared abstract.
Literals: It is a value used by the variable. This is like a constant value.
Code:
class Test {
// Main method
public static void Main(String []args)
{
int a = 102;
// octal-form literal
int b = 0145
;
// Hexa-decimal form literal
int c = 0xFace;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
}
Delegates: This is like a pointer. It is a reference type variable which holds the other methods.
2. C# Version 1.2
In this version, some enhancement has been done. They added for each loop in this version which will execute each block until an expression gets false.
3. C# Version 2.0
In this version, they have added below advance features.
Generics: Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.
Anonymous Method: This is a blank method. This is defined using the keyword delegate.
- Nullable type: Before this release, we can not define a variable as null. So this release overcomes this.
- Iterators
- Covariance and contravariance
- Getter/setter separate accessibility: We can use a getter setter for getting and setting the values.
4. C# Version 3.0
This version made C# as a formidable programming language.
- Object and collection initializers: With the help of this we can access any field without invoking constructor.
- Partial Method: As the name suggests its signature and implementations defined separately.
- Var: we can define any variable by using the keyword var.
5. C# Version 4.0
The version introduced some interesting features:
Dynamic Binding: This is like method overriding. Here the compiler does not decide the method which to call.
Code:
public class ClassA
{
public static class superclass
{
void print()
{
System.out.println("superclass.");
}
}
public static class subclass extends superclass
{
@Override
void print()
{
System.out.println("subclass.");
}
}
public static void main(String[] args)
{
superclass X = new superclass();
superclass Y= new subclass();
X.print();
Y.print();
}
}
- Named/Optional Arguments
- Generic Covariant and Contravariant
- Embedded Interop Types
Here the major feature was keyword dynamic. It overrides the compiler at run time.
6. C# Version 5.0
In this version, they added two new models for asynchronous programming.
async and await
With these, we easily retrieve information about the context. This is very helpful with long-running operations. In this async enables the keyword await. With the help of await keyword, all the things get asynchronous. So it runs synchronously till the keyword await.
7. C# Version 6.0
This version included below functionalities
- Static imports
- Expression bodied members
- Null propagator
- Await in catch/finally blocks
- Default values for getter-only properties
- Exception filters
- Auto-property initializers
- String interpolation
- name of the operator
- Index initializers
8. C# Version 7.0
This version has below advantages
Out Variables: This variable is basically used when the method has to return multiple values. The keyword out is used to pass to the arguments.
Other important aspects are
- Tuples and deconstruction.
- Ref locals and returns.
- Discards: These are write-only ready variables. Basically this is used to ignore local variables.
- Binary Literals and Digit Separators.
- Throw expressions
- Pattern matching: We can use this on any data type.
- Local functions: With the help of this function we can declare the method in the body which is already defined in the method.
- Expanded expression-bodied members.
So every version has included new features in the C# which help the developers to solve the complex problems in efficient manner. The next release will be C# 8.0.
Recommended Articles
This is a guide to C# Versions. Here we discuss the basic concept, various types of C# Versions along with examples and code implementation. You can also go through our other suggested articles –