Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Coalescing operators ,object initializers and var type in new C#
|
Introduction
Coalescing operators are introduced in c#2.0. Its look like this (??).C# 3.0 introduced object initializers. which ease construction and initialization of objects. An object initializer contains the values that the members of a newly created object should be assigned. It specifies values for one or more fields or properties of the object. In c# 3.0 one more data type var is introduced .
coalescing operator
.NET 2.0 FrameWork comes with c# 2.0, this new c# gets this capability with a new ?? operator – the null coalescing operator. So when you define nullable types, you can check for null, and replace it with another value with this simple syntax:
suppose for example1 string i=Request.QueryString[“id”]; if(i!=null) { str = Request.QueryString[“id”]; } else { str = “default”; }
in the above example It may chance for getting null value if null means I need to give some default value at this situation I need the above code. but using coalescing operators in c#2.0 it becomes easy and efficient. See this example
string i=Request.QueryString[“id”]; here u no need to write the above coding . just use this operator like this run(i ?? ”default”);
Example 2:
////// y contains some value from database y = dr[0]; int? x = y; /// this means suppose if x value is null then it value is changed to 0 otherwise it contains the original value. getRank(x??0);
The operator also works with reference types: // table can be null public static int Fill(DataTable table) { // if table is null, _defaultDataTable is used instead foreach(DataRow dataRow in table.Rows ?? _defaultDataTable.Rows) { // do work } return 0; }
Object Intializers
Properties • It consists of a sequence of member initializers, enclosed by "{" and "}" tokens and separated by commas. • Each member initializer can name only an accessible field or property of the object being initialized, followed by an equal sign (=) and an expression of the value (V) or an object initializer. • A member initializer that specifies an expression of value (V), as above, is processed in the same way as an assignment to the field or property. • A member initializer that specifies an object initializer after the equal sign is an initialization of an embedded object. The assignments in the object initializers are treated as assignments to • members of the field or property. Value type properties cannot be initialized using this mechanism. If an object-creation expression includes an object or collection initializer, it can omit the constructor argument list and enclosing parenthesis. Omitting the constructor argument list and the enclosing parenthesis is equivalent to specifying an empty argument list. An object-creation expression is executed in two steps: 1. Invoke an instance constructor, which creates the object. 2. Perform member initialization using the values specified in the object initializers. (An object initializer cannot refer to the object being initialized.) The object initializer first invokes the object's parameterless constructor and then initializes the named fields to the specified values. Fields that are not specified will have the default values. Example
Suppose you have a class Student, who can be identified by First Name and Last Name. You also have an entity Branch, which is composed of three such students. This scenario can be represented with the following C# 3.0:
using System; using System.Collections.Generic; using System.Text; using System.Query; using System.Xml.XLinq; using System.Data.DLinq;
namespace ObjectInitializers { class Program { public class Student { public string firstName; public string lastName; }
public class Branch { public Student Student1, Student2, Student3; } static void Main(string[] args) { var student1 = new Student{firstName = "phani", lastName = "kumar"}; var student2 = new Student{firstName = "raj", lastName = "kumar"}; var student3 = new Student{firstName = "ynds", lastName = "kumar"};
var sClass = new Branch{Student1 = student1, Student2 = student2, Student3 = student3}; } } }
var type This type is not like javascript var. in javascript this is used to create an object but in c# it is different
Suppose for example
In javascript var x = 5; …. .. … statements .. x = “phani”;
this is possible. That it is treated as an object only.
In C#
var x = 5
…….
// then the type of x is only int . that means it takes the type of value that has been initialized. So 5 is int type then x is int type.
///// it give error cannot convert to string … x = “phani”
Summary
In this article I explained some new concepts in c#2.0 and c# 3.0
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|