Inside Razor view use Newtonsoft JSON serialization from C# to JSON:
@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.CityList, Newtonsoft.Json.Formatting.None));
Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts
Feb 6, 2018
Aug 20, 2015
Aug 6, 2015
RePost : C# - using statement tips
When using any class that implements IDisposable it is advisable to use using statement.
Now what I didn't know is that using statement is replacement for try finally with call to dispose.
Furthermore call to dispose internally calls Close() for Streams I think.
So using statement is real champion :)
Instead of:
try{
Stream s = new Stream();
}
finally
{
s.Flush();
s.Close();
s.Dispose();
}
Just this:
using ( Stream s = new Stream())
{
}
Important!
Flush() is NOT called automatically !
So using takes care of connections and resources but does not takes care that your data will be flushed from cache.
This makes sense since it primary protects from exceptions and developer should think about Flush().
http://stackoverflow.com/questions/911408/does-stream-dispose-always-call-stream-close-and-stream-flush
Now what I didn't know is that using statement is replacement for try finally with call to dispose.
Furthermore call to dispose internally calls Close() for Streams I think.
So using statement is real champion :)
Instead of:
try{
Stream s = new Stream();
}
finally
{
s.Flush();
s.Close();
s.Dispose();
}
Just this:
using ( Stream s = new Stream())
{
}
Important!
Flush() is NOT called automatically !
So using takes care of connections and resources but does not takes care that your data will be flushed from cache.
This makes sense since it primary protects from exceptions and developer should think about Flush().
http://stackoverflow.com/questions/911408/does-stream-dispose-always-call-stream-close-and-stream-flush
Apr 29, 2013
Object cloning
Object cloning
How to perform shallow copy of complex type?
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx
How to perform shallow copy of complex type?
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx
Apr 8, 2013
Custom type comparing
You have custom type EditorColumn:
public class EditorColumns
{
public bool IsSelected { get; set; }
public string FieldName { get; set; }
public int? OrderId { get; set; }
}
I want to do lambda expression that checks whether MyCollection that is collection of EditorColumn's contains seleted MyEditorColumn of type EditorColumns.
LINQ lambda for this is: Contains
What is equality criteria for this?
How do you define that MyEditiorColumn has its matches in collection?
Since this is not value type by default .NET will use type reference.
Let's say that two EditorColumn's are equal if their fieldname's exactly match.
This has to be designed into our EditorColumn like this:
internal class FieldNameComparer : IEqualityComparer<EditorColumn>
{
public bool Equals(EditorColumn x, EditorColumn y)
{
return x.FieldName.ToLowerInvariant() == y.FieldName.ToLowerInvariant();
}
public int GetHashCode(EditorColumn obj)
{
return 0;
}
}
There is no rule but I suggest that above class is placed inside EditorColumn type def.
Now we can write something like this:
var optionalDefs = defaultColumns.Where(dc => !userDefEdCols.Contains<EditorColumn>(dc,new EditorColumn.FieldNameComparer())).ToList<EditorColumn>();
Here is some more info:
http://www.code-magazine.com/Article.aspx?quickid=100083
public class EditorColumns
{
public bool IsSelected { get; set; }
public string FieldName { get; set; }
public int? OrderId { get; set; }
}
I want to do lambda expression that checks whether MyCollection that is collection of EditorColumn's contains seleted MyEditorColumn of type EditorColumns.
LINQ lambda for this is: Contains
What is equality criteria for this?
How do you define that MyEditiorColumn has its matches in collection?
Since this is not value type by default .NET will use type reference.
Let's say that two EditorColumn's are equal if their fieldname's exactly match.
This has to be designed into our EditorColumn like this:
internal class FieldNameComparer : IEqualityComparer<EditorColumn>
{
public bool Equals(EditorColumn x, EditorColumn y)
{
return x.FieldName.ToLowerInvariant() == y.FieldName.ToLowerInvariant();
}
public int GetHashCode(EditorColumn obj)
{
return 0;
}
}
There is no rule but I suggest that above class is placed inside EditorColumn type def.
Now we can write something like this:
var optionalDefs = defaultColumns.Where(dc => !userDefEdCols.Contains<EditorColumn>(dc,new EditorColumn.FieldNameComparer())).ToList<EditorColumn>();
Here is some more info:
http://www.code-magazine.com/Article.aspx?quickid=100083
Mar 11, 2013
Enumaration revisited
Enum's can be used more efficiently and economically to describe constant values.
I tend to forget this syntax so here it is:
Example from Chess class. Elegantly describing chess piece using Flags in enum.
[Flags]
public enum PieceE : byte {
/// <summary>No piece</summary>
None = 0,
/// <summary>Pawn</summary>
Pawn = 1,
/// <summary>Knight</summary>
Knight = 2,
/// <summary>Bishop</summary>
Bishop = 3,
/// <summary>Rook</summary>
Rook = 4,
/// <summary>Queen</summary>
Queen = 5,
/// <summary>King</summary>
King = 6,
/// <summary>Mask to find the piece</summary>
PieceMask = 7,
/// <summary>Piece is black</summary>
Black = 8,
/// <summary>White piece</summary>
White = 0,
}
Explicit setting of type and value for Enum
public enum MoveTypeE : byte {
/// <summary>Normal move</summary>
Normal = 0,
/// <summary>Pawn which is promoted to a queen</summary>
PawnPromotionToQueen = 1,
/// <summary>Castling</summary>
Castle = 2,
/// <summary>Prise en passant</summary>
EnPassant = 3,
/// <summary>Pawn which is promoted to a rook</summary>
PawnPromotionToRook = 4,
/// <summary>Pawn which is promoted to a bishop</summary>
PawnPromotionToBishop = 5,
/// <summary>Pawn which is promoted to a knight</summary>
PawnPromotionToKnight = 6,
/// <summary>Pawn which is promoted to a pawn</summary>
PawnPromotionToPawn = 7,
/// <summary>Piece type mask</summary>
MoveTypeMask = 15,
/// <summary>The move eat a piece</summary>
PieceEaten = 16,
/// <summary>Move coming from book opening</summary>
MoveFromBook = 32
}
I tend to forget this syntax so here it is:
Example from Chess class. Elegantly describing chess piece using Flags in enum.
[Flags]
public enum PieceE : byte {
/// <summary>No piece</summary>
None = 0,
/// <summary>Pawn</summary>
Pawn = 1,
/// <summary>Knight</summary>
Knight = 2,
/// <summary>Bishop</summary>
Bishop = 3,
/// <summary>Rook</summary>
Rook = 4,
/// <summary>Queen</summary>
Queen = 5,
/// <summary>King</summary>
King = 6,
/// <summary>Mask to find the piece</summary>
PieceMask = 7,
/// <summary>Piece is black</summary>
Black = 8,
/// <summary>White piece</summary>
White = 0,
}
Explicit setting of type and value for Enum
public enum MoveTypeE : byte {
/// <summary>Normal move</summary>
Normal = 0,
/// <summary>Pawn which is promoted to a queen</summary>
PawnPromotionToQueen = 1,
/// <summary>Castling</summary>
Castle = 2,
/// <summary>Prise en passant</summary>
EnPassant = 3,
/// <summary>Pawn which is promoted to a rook</summary>
PawnPromotionToRook = 4,
/// <summary>Pawn which is promoted to a bishop</summary>
PawnPromotionToBishop = 5,
/// <summary>Pawn which is promoted to a knight</summary>
PawnPromotionToKnight = 6,
/// <summary>Pawn which is promoted to a pawn</summary>
PawnPromotionToPawn = 7,
/// <summary>Piece type mask</summary>
MoveTypeMask = 15,
/// <summary>The move eat a piece</summary>
PieceEaten = 16,
/// <summary>Move coming from book opening</summary>
MoveFromBook = 32
}
Feb 5, 2013
Representing proper T-SQL string of .NET float & double types
You build a dynamic T-SQL statement in C#.
There is .NET double variable.
You need correct culture independent version of your double variable.
In many European cultures decimal separator is comma and not dot.
So when you pick double value from app.GUI defined by client culture it will be:
1,01
If you inject this in T-SQL statement it is of course invalid.
To workaround this use InvariantCulture.
var result = 1E-1;
string MysqlDoubleString = Convert.ToString(result, System.Globalization.CultureInfo.InvariantCulture);
Please note that above scenario of building dynamic T-SQL is very risky and unsecure since it is prone to SQL injection attacks.
Always use classic ADO.NET SQLParameter class instead.
There is .NET double variable.
You need correct culture independent version of your double variable.
In many European cultures decimal separator is comma and not dot.
So when you pick double value from app.GUI defined by client culture it will be:
1,01
If you inject this in T-SQL statement it is of course invalid.
To workaround this use InvariantCulture.
var result = 1E-1;
string MysqlDoubleString = Convert.ToString(result, System.Globalization.CultureInfo.InvariantCulture);
Please note that above scenario of building dynamic T-SQL is very risky and unsecure since it is prone to SQL injection attacks.
Always use classic ADO.NET SQLParameter class instead.
Subscribe to:
Posts (Atom)