- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
C# 3.0 new features
In this part of the C# tutorial, we will talk about some of the new features of the C# version 3.0. Microsoft introduced this version of the C# in August 2007. Mono C# compiler fully supported the new version by July 2008.
The C# 3.0 version brought the following new features to the language:
- Implicitly typed variables
- Implicitly typed arrays
- Object & collection initializers
- Automatic properties
- Anonymous types
- Extension methods
- Query expressions
- Lambda expressions
- Expression trees
Query expressions, lambda expressions and expression trees are beyond the scope of this tutorial. They are closely connected to the LINQ. LINQ (Language Integrated Query) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.
Implicitly typed local variables & arrays
Both implicitly typed local variables & arrays are connected with the var
keyword. It is an implicit data type. In some cases, we do not have to specify the type for a variable. When the usage of the var
keyword is allowed, the compiler will find and use the type for us. The process is called type inference.
In some cases, the var
keyword is not allowed. It can be used only on a local variable. It cannot be applied on a field in a class scope. It must be declared and initialized in one statement. The variable cannot be initialized to null
.
using System; public class CSharpApp { static void Main() { int x = 34; var y = 32.3f; var name = "Jane"; Console.WriteLine(x); Console.WriteLine(y.GetType()); Console.WriteLine(name.GetType()); } }
We have a small example, where we use the var
type.
int x = 34; var y = 32.3f;
The first variable is explicitly typed, the second variable is implicitly typed. The compiler will look at the right side of the assignment and infer the type of the variable.
Console.WriteLine(y.GetType()); Console.WriteLine(name.GetType());
These two lines will check the type of the two variables.
$ ./itlv.exe 34 System.Single System.String
As we can see, the two variables use the familiar, built-in data types.
Implicitly typed arrays are arrays, in which the type of the array is inferred from the elements of the array in the array initializer by the compiler. The rules are the same as for implicitly typed local variables. Implicitly typed arrays are mostly used in query expressions together with anonymous types and object and collection initializers.
using System; public class CSharpApp { static void Main() { var items = new[] { "C#", "F#", "Python", "C" }; foreach (var item in items) { Console.WriteLine(item); } } }
An example demonstrating the implicitly typed arrays.
var items = new[] { "C#", "F#", "Python", "C" };
We again use the var
keyword. The square brackets on the left side are omitted.
foreach (var item in items) { Console.WriteLine(item); }
The foreach
loop is used to traverse the array. Note the use of the local implicitly typed item variable.
$ ./ita.exe C# F# Python C
This is the output of the example.
Object initializers
Object initializers give a new syntax for creating and initiating objects. Inside a pair of curly brackets {}
we initiate members of a class through a series of assignments. They are separated by comma character.
using System; public class Person { private string _name; private int _age; public string Name { get { return _name; } set { _name = value;} } public int Age { get { return _age; } set { _age = value;} } public override string ToString() { return String.Format("{0} is {1} years old", _name, _age); } } public class ObjectInitializers { static void Main() { Person p1 = new Person(); p1.Name = "Jane"; p1.Age = 17; Person p2 = new Person { Name="Becky", Age=18 }; Console.WriteLine(p1); Console.WriteLine(p2); } }
In the above example, we have a Person
class with two properties. We create two instances of this class. We use the old way and we also use the object initializer expression.
public string Name { get { return _name; } set { _name = value;} }
This is a Name property with set a get accessors.
Person p1 = new Person(); p1.Name = "Jane"; p1.Age = 17;
This is the old way of creating an object and initiating it with values using the field notation.
Person p2 = new Person { Name="Becky", Age=18 };
This is the object initializer. Inside curly brackets, the two members are initiated.
Collection initializers
Collection initializers are a way of initiating collections where the elements of a collection are specified inside curly brackets.
List <string> planets = new List<string>(); planets.Add("Mercury"); planets.Add("Venus"); planets.Add("Earth"); planets.Add("Mars"); planets.Add("Jupiter"); planets.Add("Saturn"); planets.Add("Uranus"); planets.Add("Neptune");
This is the classic way of initiating a collection. In the following example, we will use a collection initializer for the same generic list.
using System; using System.Collections.Generic; public class CollectionInitializers { static void Main() { List <string> planets = new List <string> {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"}; foreach (string planet in planets) { Console.WriteLine(planet); } } }
We create a generic list of planets.
List <string> planets = new List <string> {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
This is the collection initializer expression. The planet names are specified between the curly brackets. We save a some typing. We do not need to call the Add()
method for each item of the collection.
$ ./collectioninitializers.exe Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune
Here we can see the output of the collectioninitializers.exe
program.
Automatic properties
In a software project, there are lots of simple properties that only set or get some simple values. To simplify programming and to make the code shorter, automatic properties were created. Note that we cannot use automatic properties in all cases. Only for the simple ones.
using System; public class Person { public string Name { get; set; } public int Age { get; set; } } public class AutomaticProperties { static void Main() { Person p = new Person(); p.Name = "Jane"; p.Age = 17; Console.WriteLine("{0} is {1} years old", p.Name, p.Age); } }
This code is much shorter. We have a person class in which we have two properties.
public string Name { get; set; } public int Age { get; set; }
Here we have two automatic properties. There is no implementation of the accessors. And there are no member fields. The compiler will do the rest for us.
Person p = new Person(); p.Name = "Jane"; p.Age = 17; Console.WriteLine("{0} is {1} years old", p.Name, p.Age);
We normally use the properties as usual.
$ ./automatic.exe Jane is 17 years old
This is the output of the example.
Anonymous types
The Person class that we have used in the previous example is said to be a type. More precisely, it is a user defined type. The type has a name and we can explicitly create an instance of it by referring to its name. On the other hand, anonymous types are types that do not have a name. They are class types that consist of one or more public read-only properties. No other kinds of class members are allowed.
Anonymous types are allow data types to encapsulate a set of properties into a single object without having to first explicitly define a type. Anonymous types must be stored in variables declared with the var
keyword, telling the C# compiler to use type inference for the variable.
Anonymous types are created with the new keyword and object initializer. The compiler will infer the types of the properties itself. It will give the type a name, but it is only used by the compiler; it is not available to the programmer.
An anonymous type is another syntactic sugar to reduce typing. Anonymous types are often used in LINQ expressions.
using System; public class AnonymousType { static void Main() { var p = new { Name="Jane", Age=17 }; Console.WriteLine("{0} is {1} years old", p.Name, p.Age); } }
This is an anonymous type example.
var p = new { Name="Jane", Age=17 };
An anonymous object initializer declares an anonymous type and returns an instance of that type. We use the var
keyword because we do not know the type.
Console.WriteLine("{0} is {1} years old", p.Name, p.Age);
We access the properties created using the field access notation.
$ ./anonymoustype.exe Jane is 17 years old
This is the output of the example.
Extension methods
Developers often face situations in which they would like to extend an existing type, but it is not possible. For example, the class is sealed. The extension method is a workaround for such cases. Extension methods are a special kind of a static method, but they are called as if they were instance methods on the extended type. To create an extension method, we need a static class and a static method. When we call our extension method on a class, the compiler does some behind the scenes processing; it appears as if we have called the extension method on the object of a type.
It is also possible, and it was a common workaround in the past, to create special utility classes for such methods. These were mostly created as static methods of static classes. Both approaches have their advantages. It is also advised to use extension methods sparingly.
using System; public static class Util { public static string Reverse(this string input) { char[] chars = input.ToCharArray(); Array.Reverse(chars); return new String(chars); } } public class ExtentionMethod { static void Main() { string str1 = "Jane"; string str2 = str1.Reverse(); Console.WriteLine(str2); } }
In our case, we would like to add a new method to a string class. The string class is a built-in class and it is sealed. No inheritance is possible. This is why we need an extension method.
public static class Util { public static string Reverse(this string input) { char[] chars = input.ToCharArray(); Array.Reverse(chars); return new String(chars); } }
We have a Reverse()
extension method. This method reverses characters of a string variable. The method and its class must be static. The static Reverse()
method becomes an extension method, when we put the this
modifier as the first parameter of the method.
string str1 = "Jane"; string str2 = str1.Reverse();
We define and initialize a string variable. We call the Reverse()
method on this variable. Even though the method is static, we use the instance method call syntax.
$ ./extentionmethods.exe enaJ
This is the output of the extentionmethods.exe program.
In this part of the C# tutorial we have talked about new features of the C# 3.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论