Category: .net Technical


What I have learnt during these months


  1. Using generic type to standardized data flow.

  2. have multiple table needed to be inserted or deleted, Have to use Transaction.

  3. Check Null is super important,both at back end or database.

  4. when SaveChange throws an error. It is because we don’t set the columns for the table to be not null。

  5. Definitely use enum to store constant variable

  6. navigation property make developer’s life much easier because you don’t need to worry about the Joining.

  7. If you want to create the instance of dbcontext, remember to use “using” to release the garbage in memory. It can clean up the memory automatically

  8. soft delete. We will only need to set the active to false instead of deleting it all

  9. set the default value for parameters of a function by using int num = 0.

  10. use factory or Interface to decouple between each layers.

  11. Json(result.Data, JsonRequestBehavior.AllowGet) standard way of passing data to front-end in controller.

  12. If you want to have the id for the record which is just added with ORM-ef, get it after DB.SAVACHANGE().

  13. EF lazy loading (when it is transferred to list. It then generates SQL query)

  14. Don’t use generate SQL in foreach loop, it is disaster.

  15. naming convention is important. Camel case is always good.

  16. Trying to use reflector to read the source code. It will help you with the understanding of CLR.

  17. put partial view into modal is a good idea. use partial view to display a table which needed to updated, your page would not be refreshed.

C#- useful function for string reference type


String Reference type method:

bool b = a.Contains (‘x’); / / Contains () to determine whether this word contain certain string. return bool value type.

bool b = a.StartsWith (” x “); // StartsWith () check if the string starts with the character x. return Boolean value type.

bool b = a.EndsWith (” x “); // EndsWith () check if the string ends with the character x. return Boolean value type.

bool b = a.Equals ( “xx”); // Equals () to determine whether two strings are the same.

int b = a.IndexOf (“x”); // IndexOf () return the index of the char in the string.

int b = a.LastIndexOf (“x”) ; // LastIndexOF () return the last index of char in the string.

string b = a.Insert(int, “x”); insert the char at the index of you want.

bool b = string.IsNullOrEmpty (x); // IsNullOrEmpty () will check whether the string is null or empty. It will return a Boolean value.

string b=a.Remove(1,10); //Remove() delete the string from index 1 to 10 and return the rest to string b.

string [] strs = a.Split (‘x’); // split()

string strs = a.Substring (int, int); / / Substring () is also used to split the string with the index starting with first parameter and ending with the second parameter.(return string)

string str = strs.ToLower() // The ToLower () convert uppercase to lowercase

string str = strs.ToUpper = (); // the ToUpper () convert lowercase to uppercase.

string str = strs.Trim (); // Trim () clears the space on both sides of the string

Good Content to know for .Net


Q1: What is ASP.NET? What is the main difference between ASP and ASP.NET?

A: ASP.NET is Microsoft’s server-side technology for creating dynamic and user-friendly Web applications, Web sites and Web services. You can create ASP.NET applications in most of the .NET compatible languages, such as VB, C#, and J#. The ASP.NET compiles the Web pages and provides much better performance than scripting languages. The main difference between ASP and ASP.NET is that ASP is interpreted; whereas, ASP.NET is compiled.

 

Q2: What is the concept of Postback in ASP.NET? How can we identify that the Page is Post Back?

A: A postback is a request sent from a client to the server from the same page where a user is already working. It’s basically posting a complete page back to server (i.e. sending all of its data) on the same page. So, the whole page is refreshed.

Page object has an “IsPostBack” property, which can be checked to know that is the page posted back.

 

Q3: What is an assembly?

A: An assembly is one of the elements of a .NET application and is termed as a primary unit of all .NET applications. This assembly can be either a DLL or an executable file.

 

Q4: What is the difference between method overriding and method overloading?

A: Overriding involves the creation of two or more methods with the same name and same signature in different classes.

Overloading is a concept of using a method at different places with the same name and different signatures within the same class.

 

Q5: What is constructor and why we use constructor?

A: Constructor is a special method of a class, which is called automatically when the instance of a class is created. It is created with the same name as the class and initializes all class members, whenever you access the class.

The main features of a constructor are as follows:

  • Constructors do not have any return type
  • Constructors are always public
  • It is not mandatory to declare a constructor; it is invoked automatically by .NET Framework

 

Q6: What is a delegate and multicast delegates?

A: A delegate is similar to a class, used for storing the reference to a method and invoking that method at runtime, if required. Some of the examples of delegates are type-safe functions, pointers or callbacks.

A delegate object is to hold references of and invoke multiple methods. Such delegate objects are called multicast delegates or combinable delegates.

 

Q7: What is a namespace?

A: Namespace is considered as a container that contains a functionally related group of classes and other types.

 

Q8: Can you declare a private class in a namespace?

A: The classes in a namespace are internal, by default. However, you can explicitly declare them as public only and not as private, protected, or protected internal.

 

Q9: What is garbage collection?

A: Garbage collection prevents memory leaks during execution of programs. Garbage collection is a low-priority process that manages the allocation and de-allocation of memory for your application. If GC finds any object that is no longer used by the application, it frees up the memory from that object.

 

Q10: Difference between Response.Redirect and Server.Transfer?

A: In case of Response.Redirect, a new request is generated from client-side for redirected page. It’s a kind of additional round trip. As a new request is generated from the client, the new URL is visible to users in the browser after redirection.

While in the case of Server.Transfer, a request is transferred from one page to another without making a round trip from the client. For the end user, the URL remains the same in the browser even after transferring to another page.

 

Q11: Briefly explain the use of Global.asax.

A: Global.asax is basically ASP.NET Application file. It’s a place to write code for Application-level events such as Application start, Application end, Session start and end, Application error etc. raised by ASP.NET.

 

Ten things you probably do not know about JS(keep updating)


1. we need to understand scope chain and understand what outer execution we are referring to.

function a()

{

var v = 2;

b();

}

function b()

{

console.log(v);

}

var v = 1;

a();

 

result would be 1 instead of 2 because b and a and global variable are in the same execution context stack so function b is referring to global valuable var v = 1;

if we put function b inside of function a, we will have value 2 instead of 1 because function b is referring to the execution context of a.

 

2. Immediately Invoked function expression: run the function immediately when i create it using ()

var greeting = function(name){

return “Hi”+ name;

}(“Leo”);

javascript engine knows that inside a parenthesis is a expression. so we can write a function expression inside of parenthesis without naming it.

 

3. Functional statement and Functional expression

There are two different phrases.

Creation phrase is to put function and your variable which set to be undefined into memory.

When it executes, if you want to invoke your method, there exists a function in memory already so it does not matter that you invoke that above your function or below your function.

But it’s different to your variable because when it is in execution context, it will set it values you want replacing undefined with your value. So you cannot use that variable anywhere you want because you will have undefined when you use that variable above your valuable setting.

Same happens to function expression because it is a valuable of an object.

 

4. JavaScript Closure

function a(){

var arr = [];

for (var i = 0; i < 3; i++) {

arr.push(function(){

console.log(i);

});

}

return arr;

}

var fs = a();

fs[0]();

This example is a typical javascript closure.

when we assign function a to var fs, the function inside of for loop is executed. We are not executing the function inside of for loop because we are not calling it. so when we want to execute it, the value of I will be three, staying in memory after assigning function a to fs. (that is a feature of closure)

function a(){

var arr = [];

for (var i = 0; i < 3; i++) {

arr.push((function(j){

return function(){

console.log(j);

}

 

})(i));

}

return arr;

}

var fs = a();

fs[0]();

fs[1]();

fs[2]();

If we want to print zero, one, two, we need to create its own scope. Use immediately invoked expression because we lock the value of I inside of for loop and we pass i to j while for loop is running. Creating its own execution context is a best solution.

Understanding On Reflection


———————————- Reflection dynamically creates a class object in an assembly that dynamically executes methods in a class , Attributes, fields

1, the assembly is divided into. Dll and .exe

2, the correspondence between assembly and methods of reflection

  1. class corresponds to Type
  2. field corresponds to FieldInfo
  3. property corresponding tp PropertyInfo
  4. method corresponds to MethodInfo

 

#region 1.0

Reflection: dynamic assign the value to property of Pig class

private void button1_Click(object sender, EventArgs e)
{
//Pig pig = new Pig();
//pig.Name = “pigpig”;
//pig.Name

//1.0 Load the assembly of the pig class lib.dll into memory
Assembly ass = Assembly.LoadFile(@”path”);

//2.0 create pig object dynamically
object pigInstance = ass.CreateInstance(“Lib.Pig”); // like Pig pig= new Pig();

//3.0 give value to name of pig
Type PigType = ass.GetType(“Lib.Pig”);

PropertyInfo nameInfo = PigType.GetProperty(“Name”); //pig.Name
nameInfo.SetValue(pigInstance, “pig_king”);

}
#endregion

 

Learning from tasks and Merge Sort JavaScript


Do not use html tag to bind event straightaway. It will cause serious problem. Try to use class or Id

For writing JavaScript method or Jquery method, if we want to pass a element to a method, we use this.

Example:

function hello(element){

//do something with the element we just passes in we can use dom or jquery to have the access to this element

$(element).color(“red”)

//or

element.innerHTML = “Hi”

}

<button onclick=”hello(this)”>hello</button>

 

Also, for any ajax call (.get,.post,.ajax), we have to make sure all data we want to pass into a action in a controller should match the parameters of an action. Otherwise, the routing mechanism cannot locate that url.

 

Checking Null is super Important


Check Null is so important in daily programming. Developers should be aware of this super important step.

For String:

  1. we can use if statement like string str =””; str!=null;

2. string.IsNullOrEmpty(str)

For object: For Example, var animal = new Animal();

if animal != null

object animal could have so many properties. The way of checking those properties is the same as object animal.

For nullable value type. we can use int i = 1;

i.HasValue

i.GetValueOrDefault

Social Media


Categories



All Posts



Contact Info