System.IndexOutOfRangeException

The exception that is thrown when an attempt is made to access an element of an array or collection with an index that is outside its bounds.

Minimum version: >= 1.1 >= Core 1.0

Statistics

18
elmah.io logo 13

How to handle it

try
{

}
catch (System.IndexOutOfRangeException e)
{

}
try
{

}
catch (System.IndexOutOfRangeException e) when (e.Message.Contains("something"))
{

}
try
{

}
catch (System.IndexOutOfRangeException e) when (LogException(e))
{

}

private static bool LogException(Exception e)
{
    logger.LogError(...);
    return false;
}

How to avoid it

Before
var myArray = new[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
var index = 10;

try
{
    var result = myArray[index];
}
catch (IndexOutOfRangeException e)
{
}
After
var myArray = new[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
var index = 10;

if (index < myArray.Length)
{
    var result = myArray[index];
}

Links

YouTube videos

Possible fixes from StackOverflow

if (myReader["Additional"] != DBNull.Value)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}

There are two basic ways how to map bidirectional one-to-one association in NH. Let's say the classes look like this:

public class Setting
{
    public virtual Guid Id { get; set; }
    public virtual Student Student { get; set; }
}

public class Student
{
    public virtual Guid Id { get; set; }
    public virtual Setting Setting { get; set; }
}

Setting class is a master in the association ("aggregate root"). It is quite unusual but it depends on problem domain...

Primary key association

public SettingMap()
{
    Id(x => x.Id).GeneratedBy.Guid();
    HasOne(x => x.Student).Cascade.All();
}

public StudentMap()
{
    Id(x => x.Id).GeneratedBy.Foreign("Setting");
    HasOne(x => x.Setting).Constrained();
}

and a new setting instance should be stored:

        var setting = new Setting();

        setting.Student = new Student();
        setting.Student.Name = "student1";
        setting.Student.Setting = setting;
        setting.Name = "setting1";

        session.Save(setting);

Foreign key association

public SettingMap()
{
    Id(x => x.Id).GeneratedBy.Guid();
    References(x => x.Student).Unique().Cascade.All();
}

public StudentMap()
{
    Id(x => x.Id).GeneratedBy.Guid();
    HasOne(x => x.Setting).Cascade.All().PropertyRef("Student");
}

Primary key association is close to your solution. Primary key association should be used only when you are absolutely sure that the association will be always one-to-one. Note that AllDeleteOrphan cascade is not supported for one-to-one in NH.

EDIT: For more details see:

http://fabiomaulo.blogspot.com/2010/03/conform-mapping-one-to-one.html

http://ayende.com/blog/3960/nhibernate-mapping-one-to-one

You're accessing the list by different threads, but a list is not threadsafe:

You could lock the list with:

lock(listTotalCost)
    listTotalCost.Add(temp.Value);

Or use Concurrent collections.

With Dictionary<,> you have to lock both reading and writing. So both

lock( ConnectionList ) {
   ConnectionList.Add( key, res );
}

and

lock( ConnectionList ) {
   res = ConnectionList[ key ];
}

and

lock( ConnectionList ) {
   int cnt = ConnectionList.Count;
}

and

lock( ConnectionList ) {
   ConnectionList.Clear();
}

and

lock( ConnectionList ) {
   foreach ( var kv in ConnectionList ) {
      // Do things
   }
}

and so on :-)

With ConcurrentDictionary<,> you don't need any locking, but note that the syntax is a little different than the one of the Dictionary<,>

This indeed looks like a bug. The problem seems to be with the inheritance. This does work:

ReturnParameter.GetCustomAttribute<MyMark>(inherit: false)

Retrieving attributes have two code paths that work a bit differently: MemberInfo.GetCustomAttribute (older) and Attribute.GetCustomAttribute (newer and recommended). There are also the generic extension methods, which use the latter, newer approach. The difference between these is indeed the way they handle inheritance. .NET 1.0 ignored the inherit parameter for properties, events and parameters. So in order not to break things, the static methods on Attribute we introduced in .NET 2.0 (together with this bug).

Seems like they neglected to special-case the return value parameter when going up the inheritance tree (see here). You can open an issue in the GitHub repo or a Connect bug.