syntax | Dmytro Shteflyuk's Home https://kpumuk.info In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming. Tue, 08 Sep 2015 00:11:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Coalesce ?? operator in C# 2.0 https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/ https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/#comments Mon, 14 Jan 2008 19:18:27 +0000 http://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/ Operator ??, that was introduced in the .NET 2.0, takes first place in my top used C# idioms list a long time, but unfortunately it is rarely used in projects I’ve participated. Therefore these snippets could be found in production code very often: 12345public string Caption {     get { return ViewState["Caption"] != null […]

The post Coalesce ?? operator in C# 2.0 first appeared on Dmytro Shteflyuk's Home.]]>
Operator ??, that was introduced in the .NET 2.0, takes first place in my top used C# idioms list a long time, but unfortunately it is rarely used in projects I’ve participated. Therefore these snippets could be found in production code very often:

1
2
3
4
5
public string Caption
{
    get { return ViewState["Caption"] != null ? (string) ViewState["Caption"] : ""; }
    set { ViewState["Caption"] = value; }
}

Or even:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public string VisibleStatistic
{
    get
    {
        string retval = (string) ViewState["VisibleStatistic"];
        return retval == null ? "" : retval;
    }
    set
    {
        ViewState["VisibleStatistic"] = value;
    }
}

public string SelectedCategoryName
{
    get
    {
        object retval = ViewState["SelectedCategoryName"];
        if (retval != null)
            return (string) retval;
        return String.Empty;
    }
    set
    {
        ViewState["SelectedCategoryName"] = value;
    }
}

Almost 50% of this could be easily removed, and you will get beautiful and clear code, just right after you will understand what the hell is operator ??. Here is information from MSDN:

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

Easy and clean explanation. Let’s try to rewrite all these examples using this operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public string Caption
{
    get { return (string) ViewState["Caption"] ?? String.Empty; }
    set { ViewState["Caption"] = value; }
}

public string VisibleStatistic
{
    get { return (string) ViewState["VisibleStatistic"] ?? String.Empty; }
    set { ViewState["VisibleStatistic"] = value; }
}

public string SelectedCategoryName
{
    get { return (string) ViewState["SelectedCategoryName"] ?? String.Empty; }
    set { ViewState["SelectedCategoryName"] = value; }
}

In case of value-types this approach will not work: you will get NullReferenceException. But do not worry — in this case we can use Nullable-types. Before:

1
2
3
4
5
public int FirstWidth
{
    get { return ViewState["FirstWidth"] != null ? (int) ViewState["FirstWidth"] : 0; }
    set { ViewState["FirstWidth"] = value; }
}

After:

1
2
3
4
5
public int FirstWidth
{
    get { return (int?) ViewState["FirstWidth"] ?? 0; }
    set { ViewState["FirstWidth"] = value; }
}

When you are using ?? operator, expression will be evaluated in the left to right order, so you can use something like this:

1
string name = FirstName ?? LastName ?? "Anonymous";

Of course, it looks more readably than the:

1
2
3
4
5
6
7
string name;
if (FirstName != null)
    name = FirstName;
else if (LastName != null)
    name = LastName;
else
    name = "Anonymous";

And a fortiori than:

1
2
3
string name = FirstName != null
        ? FirstName
        : (LastName != null ? LastName : "Anonymous");

Add it to your armoury!

The post Coalesce ?? operator in C# 2.0 first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/feed/ 18
CodeColorer updated to version 0.5.1 https://kpumuk.info/wordpress/codecolorer-update-to-version-051/ Sun, 01 Apr 2007 14:29:37 +0000 http://kpumuk.info/wordpress/codecolorer-update-to-version-051/ Right after publishing my previous post about WordPress plugins updates, I found that GeSHi library has been updated, therefor I have updated CodeColorer too. From GeSHi news: This release adds a couple of new languages, X++ and Rails, and fixes a few bugs in other languages. Ruby support is improved also. Here is the example […]

The post CodeColorer updated to version 0.5.1 first appeared on Dmytro Shteflyuk's Home.]]>
Right after publishing my previous post about WordPress plugins updates, I found that GeSHi library has been updated, therefor I have updated CodeColorer too.

From GeSHi news:

This release adds a couple of new languages, X++ and Rails, and fixes a few bugs in other languages. Ruby support is improved also.

Here is the example of Rails syntax highlighting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class User < ActiveRecord::Base
  include AccessManager
 
  attr_accessible :email, :first_name, :last_name, :country, :birthday, :home_page, :preferred_name

  has_many :videos
                         
  validates_presence_of :login, :role, :email
  validates_format_of :email, :with => /^[^@]+@.+\..+$/
  validates_presence_of :password :if => :validate_password?
  validates_length_of :password, :in => 5..100, :if => :validate_password?

  def initialize(attributes = nil)
    super
    @new_password = false
  end
 
  def self.authorize(login, password)
    User.find :first, :conditions => ['(login = ? OR email = ?) AND password = ? AND is_active = 1', login, login, password]
  end
 
  protected
 
    def validate_password?
      @new_password
    end
end

Let’s update!

The post CodeColorer updated to version 0.5.1 first appeared on Dmytro Shteflyuk's Home.]]>
WordPress plugins updates https://kpumuk.info/wordpress/wordpress-plugins-updates/ Sun, 01 Apr 2007 14:29:48 +0000 http://kpumuk.info/development/wordpress-plugins-updates/ Today I finished reorganization of my blog and now all my WordPress plugins moved to separate page. Check it if you are using my plugins in your blog. In this post I’ll describe which plugins I have at this moment. sCategory Permalink Plugin allows to select category which will be used to generate permalink on […]

The post WordPress plugins updates first appeared on Dmytro Shteflyuk's Home.]]>
Today I finished reorganization of my blog and now all my WordPress plugins moved to separate page. Check it if you are using my plugins in your blog. In this post I’ll describe which plugins I have at this moment.

sCategory Permalink

Plugin allows to select category which will be used to generate permalink on post edit page. Use custom permalink option %scategory% on Options/Permalinks options page. Current version is 0.2.1. more »

CodeColorer

CodeColorer is the plugin which allows you to insert code snippets into the post with nice syntax highlighting. Current version is 0.5.0. more »

Ad Rotator

This widget is intended to display random HTML code from a given group of HTML-chunks on sidebar. Basically it shows different HTML every time you requesting page. Current version is 1.0.1. more »

The post WordPress plugins updates first appeared on Dmytro Shteflyuk's Home.]]>