using System; public class MainClass{ public static void Main() { int j = 0198; // Not a decimal number; erroneous octal constant Console.WriteLine(j); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Leading zero specifies octal constant, not decimal
using System; public class MainClass{ public static void Main() { int k = 0365; // Leading zero specifies octal constant, not decimal Console.WriteLine(k); } }
To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7.
using System; public class MainClass{ public static void Main() { //The digits 8 and 9 are errors in specifying an octal constant. int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit Console.WriteLine(i); Console.WriteLine(j); } }
Converts specified value to nullable value.
#region License // Copyright (c) 2007 James Newton-King // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. #endregion using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.Data.SqlTypes; namespace Newtonsoft.Utilities.Database { public static class DatabaseUtils { /// <summary> /// Converts specified value to nullable value. /// </summary> /// <param name="value">The value.</param> /// <returns></returns> public static T? ConvertToNullableValue<T>(object value) where T : struct { if (value == null) { return null; } else if (value == DBNull.Value) { return null; } else if (value is string && string.IsNullOrEmpty((string)value)) { return null; } else { if (!(value is T)) { try { value = Convert.ChangeType(value, typeof(T)); } catch (Exception e) { throw new ArgumentException("Value is not a valid type.", "value", e); } } return new T?((T)value); } } /// <summary> /// Determines whether the specified value is null. /// </summary> /// <param name="value">The value.</param> /// <returns> /// <c>true</c> if the specified value is null; otherwise, <c>false</c>. /// </returns> public static bool IsNull(object value) { if (value == null) return true; if (value is INullable && ((INullable)value).IsNull) return true; if (value == DBNull.Value) return true; return false; } } }
Determines whether the specified value is null.
#region License // Copyright (c) 2007 James Newton-King // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. #endregion using System; using System.Data.SqlTypes; namespace Newtonsoft.Utilities.Database { public static class SqlUtils { public static object ToValue(INullable nullableValue) { if (nullableValue == null) return null; else if (nullableValue is SqlInt32) return ToValue((SqlInt32)nullableValue); else if (nullableValue is SqlInt64) return ToValue((SqlInt64)nullableValue); else if (nullableValue is SqlBoolean) return ToValue((SqlBoolean)nullableValue); else if (nullableValue is SqlString) return ToValue((SqlString)nullableValue); else if (nullableValue is SqlDateTime) return ToValue((SqlDateTime)nullableValue); throw new Exception(string.Format("Unsupported INullable type: {0}", nullableValue.GetType())); } /// <summary> /// Determines whether the specified value is null. /// </summary> /// <param name="value">The value.</param> /// <returns> /// <c>true</c> if the specified value is null; otherwise, <c>false</c>. /// </returns> public static bool IsNull(object value) { if (value == null) return true; if (value is INullable && ((INullable)value).IsNull) return true; if (value == DBNull.Value) return true; return false; } /// <summary> /// Converts SqlType to .NET value. /// </summary> /// <param name="sqlValue">The SQL value.</param> /// <returns></returns> public static int? ToValue(SqlInt32 sqlValue) { if (IsNull(sqlValue)) return null; else return sqlValue.Value; } /// <summary> /// Converts SqlType to .NET value. /// </summary> /// <param name="sqlValue">The SQL value.</param> /// <returns></returns> public static long? ToValue(SqlInt64 sqlValue) { if (IsNull(sqlValue)) return null; else return sqlValue.Value; } /// <summary> /// Converts SqlType to .NET value. /// </summary> /// <param name="sqlValue">The SQL value.</param> /// <returns></returns> public static string ToValue(SqlString sqlValue) { if (IsNull(sqlValue)) return null; else return sqlValue.Value; } /// <summary> /// Converts SqlType to .NET value. /// </summary> /// <param name="sqlValue">The SQL value.</param> /// <returns></returns> public static DateTime? ToValue(SqlDateTime sqlValue) { if (IsNull(sqlValue)) return null; else return sqlValue.Value; } /// <summary> /// Converts SqlType to .NET value. /// </summary> /// <param name="sqlValue">The SQL value.</param> /// <returns></returns> public static bool? ToValue(SqlBoolean sqlValue) { if (IsNull(sqlValue)) return null; else return sqlValue.Value; } public static T? ToValue<T>(object value) where T : struct { if (IsNull(value)) return null; else return new T?((T)value); } public static SqlInt32 FromValue(int? nullableValue) { return (nullableValue != null) ? new SqlInt32(nullableValue.Value) : new SqlInt32(); } public static SqlDateTime FromValue(DateTime? nullableValue) { return (nullableValue != null) ? new SqlDateTime(nullableValue.Value) : new SqlDateTime(); } public static SqlBoolean FromValue(bool? nullableValue) { return (nullableValue != null) ? new SqlBoolean(nullableValue.Value) : new SqlBoolean(); } } }
Checks if a value is in a range (inclusive)
//http://isotopescreencapture.codeplex.com/ //The MIT License (MIT) namespace Isotope.Math { public static class MathUtil { /// <summary> /// Checks if a value is in a range (inclusive) /// </summary> /// <param name="val"></param> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> public static bool IsInRange(double val, double min, double max) { return ((min <= val) && (val <= max)); } /// <summary> /// Checks if a value is in the range 0.0 to 1.0 inclusive /// </summary> /// <param name="val"></param> /// <returns></returns> public static bool IsInRange_0_1(double val) { return IsInRange(val, 0.0, 1.0); } } }
Sparse Int Range
//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
using System.Collections.Generic;
using System.Linq;
namespace Isotope.Collections.Ranges
{
public static partial class EnumerableUtil
{
public static IEnumerable
{
yield return item;
}
///
///
///
///
///
///
public static IEnumerable
{
// for non-positive number of steps, yield no points
if (steps < 1)
{
yield break;
}
// for exactly 1 step, yield the start value
if (steps == 1)
{
yield return start;
yield break;
}
// for exactly 2 stesp, yield the start value, and then the end value
if (steps == 2)
{
yield return start;
yield return end;
yield break;
}
// for 3 steps or above, start yielding the segments
// notice that the start and end values are explicitly returned so that there
// is no possibility of rounding error affecting their values
int segments = steps - 1;
double total_length = end - start;
double stepsize = total_length / segments;
yield return start;
for (int i = 1; i < (steps - 1); i++)
{
double p = start + (stepsize * i);
yield return p;
}
yield return end;
}
public static IEnumerable
IEnumerable
int group_size,
System.Func
System.Action
{
if (items == null)
{
throw new System.ArgumentNullException(“items”);
}
if (group_size < 1)
{
throw new System.ArgumentOutOfRangeException("group_size");
}
if (func_new_col == null)
{
throw new System.ArgumentNullException("func_new_col");
}
if (func_add == null)
{
throw new System.ArgumentNullException("func_add");
}
int cur_group_size = 0;
TGROUP cur_group = null;
foreach (var item in items)
{
if (cur_group_size == 0)
{
if (cur_group == null)
{
cur_group = func_new_col(group_size);
}
else
{
throw new System.InvalidOperationException();
}
}
func_add(cur_group, cur_group_size, item);
cur_group_size++;
if (cur_group_size == group_size)
{
yield return cur_group;
cur_group = null;
cur_group_size = 0;
}
}
if (cur_group_size > 0)
{
if (cur_group == null)
{
throw new System.InvalidOperationException();
}
yield return cur_group;
}
}
public static IEnumerable
{
if (items == null)
{
throw new System.ArgumentNullException(“items”);
}
if (group_sizes == null)
{
throw new System.ArgumentNullException(“group_sizes”);
}
if (func_new_col == null)
{
throw new System.ArgumentNullException(“func_new_col”);
}
if (func_add == null)
{
throw new System.ArgumentNullException(“func_add”);
}
int total_group_sizes = group_sizes.Sum();
if (total_group_sizes != items.Count)
{
throw new System.ArgumentException(“group_sizes must account for all items”);
}
int items_added = 0;
for (int group_index = 0; group_index < group_sizes.Count; group_index++)
{
int cur_group_size = group_sizes.ElementAt(group_index);
if (cur_group_size < 0)
{
throw new System.ArgumentException("group_sizes contains a negative numver");
}
var cur_group = func_new_col(cur_group_size);
for (int row_index = 0; row_index < cur_group_size; row_index++)
{
var cur_item = items.ElementAt(items_added);
func_add(cur_group, row_index, cur_item);
items_added++;
}
yield return cur_group;
}
}
public static IDictionary
{
if (items == null)
{
throw new System.ArgumentNullException(“items”);
}
if (func_get_key == null)
{
throw new System.ArgumentNullException(“func_get_key”);
}
var dic = new Dictionary
foreach (var item in items)
{
var key = func_get_key(item);
List
bool found = dic.TryGetValue(key, out list);
if (!found)
{
list = new List
dic[key] = list;
}
list.Add(item);
}
return dic;
}
public static IDictionary
{
IEqualityComparer
return Bucketize
}
public static IDictionary
{
if (items == null)
{
throw new System.ArgumentNullException(“items”);
}
if (func_get_key == null)
{
throw new System.ArgumentNullException(“func_get_key”);
}
var dic = new Dictionary
foreach (var item in items)
{
var key = func_get_key(item);
int old_value = 0;
bool found = dic.TryGetValue(key, out old_value);
if (!found)
{
dic[key] = 1;
}
else
{
dic[key] = old_value + 1;
}
}
return dic;
}
public static IDictionary
{
var dic = Histogram(items, i => i, null);
return dic;
}
public static List> Chunkify
{
var chunks = new List>();
List
Chunkify(items, chunksize,
() =>
{
cur_chunk = new List
},
item =>
{
cur_chunk.Add(item);
});
return chunks;
}
public static void Chunkify
{
if (items == null)
{
throw new System.ArgumentNullException(“items”);
}
if (chunksize < 1)
{
throw new System.ArgumentOutOfRangeException("chunksize");
}
int item_count = 0;
int curchunk_size = 0;
foreach (T item in items)
{
if ((item_count % chunksize) == 0)
{
create_chunk();
curchunk_size = 0;
}
add_item(item);
item_count++;
curchunk_size++;
}
}
}
public struct IntRange
{
public readonly int _Lower;
public readonly int _Length;
public int Lower
{
get { return this._Lower; }
}
public int Upper
{
get { return this.Lower + this.Length - 1; }
}
public int Length
{
get { return this._Length; }
}
private IntRange(int lower, int length)
{
this._Lower = lower;
this._Length = length;
if (this.Lower > this.Upper)
{
throw new System.OverflowException();
}
}
///
///
///
/// (0,0) -> [0]
/// (0,1) -> [0,1]
/// (0,5) -> [0,1,2,3,4,5]
/// (2,5) -> [2,3,4,5]
///
///
///
///
public static IntRange FromEndpoints(int lower, int upper)
{
return new IntRange(lower, upper – lower + 1);
}
///
///
///
///
public static IntRange FromInteger(int value)
{
return new IntRange(value, 1);
}
///
///
///
/// (2,0) -> []
/// (2,1) -> [2]
/// (2,2) -> [2,3]
/// (2,3) -> [2,3,4]
/// (2,4) -> [2,3,4,5]
///
///
///
///
public static IntRange FromLower(int lower, int length)
{
if (length < 0)
{
throw new System.ArgumentOutOfRangeException("length", "must be >= 0″);
}
return new IntRange(lower, length);
}
public override string ToString()
{
var invariant_culture = System.Globalization.CultureInfo.InvariantCulture;
return string.Format(invariant_culture, “Range({0},{1})”, this.Lower, this.Upper);
}
private static bool _intersects(IntRange range1, IntRange range2)
{
return ((range1.Lower <= range2.Lower) && (range1.Upper >= range2.Lower));
}
///
///
/// the other range
///
public bool Intersects(IntRange range)
{
return (_intersects(this, range) || _intersects(range, this));
}
private static int Delta = 1;
private static bool _touches(IntRange range1, IntRange range2)
{
var val = (range1.Upper + Delta) == range2.Lower;
return val;
}
///
///
/// the other range
///
public bool Touches(IntRange range)
{
var val = _touches(this, range) || _touches(range, this);
return val;
}
///
///
/// the integet
///
public bool Contains(int n)
{
return ((this.Lower <= n) && (n <= this.Upper));
}
///
/// for example (0,2) amd (3,7) will yield (0,7)
///
/// the other range
///
public IntRange JoinWith(IntRange range)
{
if (this.Intersects(range) || this.Touches(range))
{
int new_Upper = System.Math.Max(this.Upper, range.Upper);
int new_Lower = System.Math.Min(this.Lower, range.Lower);
return IntRange.FromEndpoints(new_Lower, new_Upper);
}
else
{
throw new System.ArgumentException();
}
}
///
///
///
public IEnumerable
{
get
{
for (int i = this.Lower; i <= this.Upper; i++)
{
yield return i;
}
}
}
}
public class SparseIntRange
{
private List
public SparseIntRange()
{
this.clear();
}
public IEnumerable
{
get
{
foreach (var range in this.ranges)
{
yield return range;
}
}
}
public IEnumerable
{
get
{
foreach (var rng in this.Ranges)
{
foreach (int i in rng.Values)
{
yield return i;
}
}
}
}
private void clear()
{
this.ranges = new List
}
public int RangeCount
{
get { return this.ranges.Count; }
}
public void Add(int n)
{
var rng = IntRange.FromInteger(n);
this.Add(rng);
}
public void AddInclusive(int lower, int upper)
{
var rng = IntRange.FromEndpoints(lower, upper);
this.Add(rng);
}
public void Add(IntRange range)
{
var left = new List
var right = new List
foreach (var rng in this.ranges)
{
if (range.Intersects(rng) || range.Touches(rng))
{
range = range.JoinWith(rng);
}
else if (rng.Upper < range.Lower)
{
left.Add(rng);
}
else if (range.Upper < rng.Lower)
{
right.Add(rng);
}
else
{
throw new System.InvalidOperationException("Internal Error");
}
}
this.ranges = left.Concat(EnumerableUtil.Single(range)).Concat(right).ToList();
}
public int Lower
{
get
{
if (this.ranges.Count < 1)
{
throw new System.InvalidOperationException("There are no ranges");
}
return this.ranges[0].Lower;
}
}
public int Upper
{
get
{
if (this.ranges.Count < 1)
{
throw new System.InvalidOperationException("empty");
}
return this.ranges[this.ranges.Count - 1].Upper;
}
}
public int Count
{
get
{
int length = this.ranges.Aggregate(0, (old, rng) => old + rng.Length);
return length;
}
}
public void Remove(int value)
{
var rng = IntRange.FromInteger(value);
this.Remove(rng);
}
public void RemoveInclusive(int lower, int upper)
{
var rng = IntRange.FromEndpoints(lower, upper);
this.Remove(rng);
}
public void Remove(IntRange range)
{
// if the range doesn't intersect this collection do nothing
if (!range.Intersects(IntRange.FromEndpoints(this.Lower, this.Upper)))
{
return;
}
var middle = new List
foreach (var S in this.ranges)
{
if (!range.Intersects(S))
{
middle.Add(S);
continue;
}
if ((range.Lower <= S.Lower) && (range.Upper >= S.Upper))
{
// disregard S completely
continue;
}
if (range.Lower > S.Lower)
{
if (S.Lower <= (range.Lower - 1))
{
var X = IntRange.FromEndpoints(S.Lower, range.Lower - 1);
middle.Add(X);
}
}
if (range.Upper <= S.Upper)
{
if ((range.Upper + 1) <= S.Upper)
{
var X = IntRange.FromEndpoints(range.Upper + 1, S.Upper);
middle.Add(X);
}
}
else
{
throw new System.InvalidOperationException("internal error");
}
}
this.ranges = middle;
}
public IntRange? FindRangeContainingNumber(int n)
{
foreach (var rng in this.ranges)
{
if (rng.Contains(n))
{
return rng;
}
}
return null;
}
public bool Contains(int n)
{
var rng = this.FindRangeContainingNumber(n);
return rng != null ? true : false;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.AppendFormat("{0}(", this.GetType().Name);
foreach (var rng in this.ranges)
{
sb.Append(rng.ToString());
}
sb.Append(")");
return sb.ToString();
}
}
}
[/csharp]